Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops Java循环帮助,如何编写循环_Loops - Fatal编程技术网

Loops Java循环帮助,如何编写循环

Loops Java循环帮助,如何编写循环,loops,Loops,需要编写一个循环,提示用户输入大于1的值,打印从1到用户给您的数字(即,如果用户输入5,则程序将打印“1 2 3 4 5”) 我让循环使用代码打印出1-10之间的数字: public class Loop { public static void main(String args[]) { int num = 0; while(num < 10) { num = num + 1; System.out.print(" "

需要编写一个循环,提示用户输入大于1的值,打印从1到用户给您的数字(即,如果用户输入5,则程序将打印“1 2 3 4 5”)

我让循环使用代码打印出1-10之间的数字:

public class Loop {

   public static void main(String args[]) {
      int num = 0;

      while(num < 10) {
         num = num + 1;
         System.out.print(" " + num);
      }
   }
}
公共类循环{
公共静态void main(字符串参数[]){
int num=0;
while(num<10){
num=num+1;
系统输出打印(“+num”);
}
}
}
要完成我正在考虑的代码,请将其修改为:

public class Loop {

   public static void main(String args[]) {
      int num = 2;
      if(num <1){
         System.out.print(" Num has to be greater than one");

      while(num < 10) {
         num = num - 1;// subtract instead of add
         System.out.print(" " + num);
      }
   }
}
公共类循环{
公共静态void main(字符串参数[]){
int num=2;

如果(num必须从1循环到指定的数字,那么引入一个从1到num递增的新变量

例如,将while循环替换为:

for (int i=1; i<num; ++i) {
    System.out.print("  "+i);
}

对于(int i=1;i您必须在打印之前验证您的号码

import java.util.Scanner;

//this alows you to enter your number on keyboard and validates it (>0)
Scanner sc = new Scanner(System.in);
int num = 0;
While (num <=0)
{
system.out.println("Enter a number greater then 0");
num = sc.nextInt();

}

for (int i=1;i<=num;i++){
system.out.println(" "+i);
}
import java.util.Scanner;
//这允许您在键盘上输入您的号码并验证它(>0)
扫描仪sc=新的扫描仪(System.in);
int num=0;
而(num则减少“num”在打印之前。因此,当您点击print语句num时,您在循环中的第一次是1。您需要在进入循环之前执行递增操作:将1添加到num,以便在您第一次打印时将其递减为2,或者您需要交换递减和print语句,以便在您修改num之前将其打印。这一秒第二,你必须考虑过多的减量,以便在最后打印0

无论哪种方式,您都希望在修改中减少,因此需要在while语句中检查num是否为0或更大。在这种情况下,for循环可能是有益的


对你来说,一个好的技巧是通过手工记录每一步后每个变量的值来逐步完成你的逻辑。

如果
数字
2
,循环将一直持续下去,因为
2
for(int i=start;我非常有帮助,请查看我对我的问题的编辑(更新--)需要一些交换值的帮助我从未注意到此响应。我真的不理解您的更新?您可能想要类似于int start=1;int stop=3;while(start=stop){sysout(start);start--}。我不会尝试从“stop”变量开始,然后转到“start”。。是否可以交换start和stop?
import java.util.Scanner;

//this alows you to enter your number on keyboard and validates it (>0)
Scanner sc = new Scanner(System.in);
int num = 0;
While (num <=0)
{
system.out.println("Enter a number greater then 0");
num = sc.nextInt();

}

for (int i=1;i<=num;i++){
system.out.println(" "+i);
}
public static void main(String[] args) {

    // Scanner s = new Scanner(System.in);
    // int num = s.nextInt();

    int num = 2;
    if (num < 1)
      System.out.print(" Num has to be greater than one");
    else
      while (num >= 0 && num < 10) {
        System.out.print(num-- + " ");
      }
  }
for (int i = start; i <= stop; ++i)
    system.out.println(i + " ");