我似乎无法将 double 转换为 int

     int a = 1;

  double b = 1.0;

  double l = 10;


for (int i=1;i<=3;i++){


  a=(int) b;

   System.out.println("");

   for (a=a;a<=l;a++){

       System.out.print(a+" ");           

   }

   l=l*2;

   System.out.println("");


   for ( b=a;b<=l;b++){

System.out.print(b+" ");


a=(int) b;

不起作用请帮忙。输出应显示:


1 2 3 4 5 6 7 8 9 10 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0 21 22 23 24 25 26 27...


每次从整数到双倍交替,直到达到 100。


陪伴而非守候
浏览 183回答 2
2回答

有只小跳蛙

从给定代码的外观来看,您似乎忘记在末尾添加 2 个右括号。这是我添加右括号并将它们放在主函数中时的代码:public static void main (String[] args)&nbsp; {&nbsp; &nbsp; int a = 1;&nbsp; &nbsp; double b = 1.0;&nbsp; &nbsp; double l = 10;&nbsp; &nbsp; for (int i=1;i<=3;i++){&nbsp; &nbsp; &nbsp; a=(int) b;&nbsp; &nbsp; &nbsp; System.out.println("");&nbsp; &nbsp; &nbsp; for (a=a;a<=l;a++){&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(a+" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; l=l*2;&nbsp; &nbsp; &nbsp; System.out.println("");&nbsp; &nbsp; &nbsp; for ( b=a;b<=l;b++){&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(b+" ");&nbsp; &nbsp; &nbsp; &nbsp; a=(int) b;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }}这是您的程序的输出:1 2 3 4 5 6 7 8 9 10&nbsp;11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0&nbsp;21.0 22.0 23.0 24.0 25.0 26.0 27.0 28.0 29.0 30.0 31.0 32.0 33.0 34.0 35.0 36.0 37.0 38.0 39.0 40.0&nbsp;41.0 42.0 43.0 44.0 45.0 46.0 47.0 48.0 49.0 50.0 51.0 52.0 53.0 54.0 55.0 56.0 57.0 58.0 59.0 60.0 61.0 62.0 63.0 64.0 65.0 66.0 67.0 68.0 69.0 70.0 71.0 72.0 73.0 74.0 75.0 76.0 77.0 78.0 79.0 80.0虽然输出不是您想要的,但它表明您从 double 到 int 的转换有效。问题可能是语法错误。如果您只对输出感兴趣,我可以提供另一种方法:public static void main (String[] args) throws java.lang.Exception&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i < 100; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (((i-1) / 10) % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(Integer.toString(i) + ".0");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(" ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(100.0);&nbsp; &nbsp; }

梦里花落0921

尝试这个:for(int i = 0 ; i < 100 ; i+=10){&nbsp; &nbsp; &nbsp; &nbsp; for(int j = 1 ; j <= 10 ; j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((i/10)%2 != 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double d = i+j;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(d+" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print((i+j)+" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }您也可以这样做,而不是将其转换为双精度,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((i/10)%2 != 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print((i+j)+".0 ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java