如何在Java中使用嵌套循环只打印一次单个数字?
在我的Java代码中,除了在代码的最后,一切都运行良好。所以基本上我不知道如何打印出相同的用户号码。例如,我提示用户输入起始数字和结束数字(整数)。假设用户输入相同的整数“10”作为起始数字,“10”作为结束数字。我希望输出仅为“10”,只打印一次。我已经尝试了所有我能想到的方法,包括尝试While循环、Do While循环和For循环,但我就是想不出来 ------------------------下面是JAVA代码-------------------------------------------如何在Java中使用嵌套循环只打印一次单个数字?,java,for-loop,while-loop,nested,do-while,Java,For Loop,While Loop,Nested,Do While,在我的Java代码中,除了在代码的最后,一切都运行良好。所以基本上我不知道如何打印出相同的用户号码。例如,我提示用户输入起始数字和结束数字(整数)。假设用户输入相同的整数“10”作为起始数字,“10”作为结束数字。我希望输出仅为“10”,只打印一次。我已经尝试了所有我能想到的方法,包括尝试While循环、Do While循环和For循环,但我就是想不出来 ------------------------下面是JAVA代码--------------------------------------
import java.util.Scanner;
public class LoopsAssignment {
public static void main(String[] args) {
// input Scanner
Scanner input = new Scanner(System.in);
// ask user for a starting number and a ending number
System.out.println("Now I'll print whatever numbers you'd like!");
System.out.println("Give me a starting number: ");
startNum = input.nextInt();
System.out.println("Give me an ending number: ");
endNum = input.nextInt();
// count the users range of numbers
System.out.println("I counted your range of numbers: ");
int a = startNum;
int b = endNum;
while (a <= b) {
System.out.println(a);
a = a + 1;
}
while (a >= b) {
System.out.println(a);
a = a - 1;
}
while (a == b) {
System.out.println(a);
}
}
}
import java.util.Scanner;
公共类循环签名{
公共静态void main(字符串[]args){
//输入扫描器
扫描仪输入=新扫描仪(System.in);
//向用户询问起始号码和结束号码
System.out.println(“现在我将打印您想要的任何数字!”);
System.out.println(“给我一个起始号码:”);
startNum=input.nextInt();
System.out.println(“给我一个结束号码:”);
endNum=input.nextInt();
//计算用户的数字范围
System.out.println(“我计算了您的数字范围:”);
int a=startNum;
int b=endNum;
while(a=b){
系统输出打印项次(a);
a=a-1;
}
while(a==b){
系统输出打印项次(a);
}
}
}
---------------------下发-----------------------------------------------------
import java.util.Scanner;
public class LoopsAssignment {
public static void main(String[] args) {
// input Scanner
Scanner input = new Scanner(System.in);
// ask user for a starting number and a ending number
System.out.println("Now I'll print whatever numbers you'd like!");
System.out.println("Give me a starting number: ");
startNum = input.nextInt();
System.out.println("Give me an ending number: ");
endNum = input.nextInt();
// count the users range of numbers
System.out.println("I counted your range of numbers: ");
int a = startNum;
int b = endNum;
while (a <= b) {
System.out.println(a);
a = a + 1;
}
while (a >= b) {
System.out.println(a);
a = a - 1;
}
while (a == b) {
System.out.println(a);
}
}
}
现在我要打印你想要的任何数字!
给我一个起始号码:
10
给我一个结束号码:
10
我数了数你的数字范围:
10
11
十,
----jGRASP:操作完成。您可以使用
进行循环
:
public static void printRange(int minInclusive, int maxInclusive) {
for (; minInclusive <= maxInclusive; minInclusive++)
System.out.println(minInclusive);
}
public静态无效打印范围(int-minInclusive,int-maxInclusive){
因为(;minInclusive所以你要么在倒计时,要么就只有一个
所以
< >或在<<代码>的中间设置< <代码> > <代码>循环。也有<代码> += ,以及一些可以使其更常规的东西。
int step = endNum>startNum ? +1 : -1;
for (int i=startNum; ; i+=step) {
System.out.println(i);
if (i == endNum) {
break;
}
}
您可以按如下方式重新构造代码:
while (a < b) {
System.out.println(a);
a = a + 1;
}
while (a > b) {
System.out.println(a);
a = a - 1;
}
if (a == b) {
System.out.println(a);
}
while(ab){
系统输出打印项次(a);
a=a-1;
}
如果(a==b){
系统输出打印项次(a);
}
问题在于前两个,而循环使用“=
”和"对不起,我应该提到我需要前两个while循环,因为如果它运行,我需要计算用户的数字范围……例如:startNum=1、endNum=10或startNum=10、endNum=1或startNum=10、endNum=10,当我去掉无限循环中的等号时,我需要编码来打印该范围。例如:startNum=1,endNum=10//OUTPUT=“1 2 3 4 5 6 7 8 9 10”@DMJ你真的尝试过吗?注意使用了“if”而不是最后一个“while”。我想你忘了将最后一个“while”更改为“if”。它起作用了!你是对的,因为我没有将它更改为if语句。我知道我的逻辑也出了什么问题,因为“>=”和"