试图让这个循环在20行之后结束



执行20次后,有人能帮助我停止这个循环吗?

我必须让代码在打印完20双后停止。用户输入华氏温度,然后代码应该打印出华氏温度加及其摄氏度计数器部分。然后应该在20对时停止执行

import java.text.DecimalFormat;
import java.util.Scanner;
public class Temperature 
{
public static void main (String[] args)
{
int Fah, i, count = 10;
int rows = 20;
double Cel, uplimit, lowlimit;
final int PER_LINE = 10;

uplimit = 212;
lowlimit = -459;

Scanner scan = new Scanner (System.in);
System.out.print("Enter the temperature in farenheit: ");
Fah = scan.nextInt();

//Gives error if you enter a number below -459 or aboce 212
while (Fah < lowlimit || Fah > uplimit)
{System.out.print("Invalid input. Please reenter: ");
Fah= scan.nextInt();
}
//Allows for max of 20 executions of the loop
int floop = Fah * 21;
double loopval;

for ( int n = 1 ; n <= 20 ; n = n+1 )
{
for (loopval = Fah; loopval < 212; loopval = loopval + 10)
{
// Celsius  = (Fahrenheit - 32)*5/9;
double cloop = (loopval - 32)*5/9;

DecimalFormat fmt = new DecimalFormat("0.##");
System.out.println("Fareheit: " + loopval + " t Celsius: " + fmt.format(cloop));

// Print a specific number of values per line of output
if (count % PER_LINE == 0);

}
}
}
}

注释中提到的问题是使用嵌套循环。

一种可能的解决方案是将它们组合成一个单独的循环:

int n;
for (n = 1, loopval = Fah; n <= 20 && loopval < 212; ++n, loopval += 10) {
...
}

@Alexander,您希望的外部执行二十次。但我认为Fah的初始值将决定它,因为你不希望它超过212(但评论中的讨论清楚地表明,它可以超过212。(

因此,例如,对于您当前的代码,如果我将Fah的初始值设为121(见下面的输出(,它会执行外部的20次,但内部的执行次数少于的20次

我刚刚打印了左边的序列号,看编号。

Enter the temperature in farenheit: 121
1Fareheit: 121.0         Celsius: 49.44
1Fareheit: 131.0         Celsius: 55
1Fareheit: 141.0         Celsius: 60.56
1Fareheit: 151.0         Celsius: 66.11
1Fareheit: 161.0         Celsius: 71.67
1Fareheit: 171.0         Celsius: 77.22
1Fareheit: 181.0         Celsius: 82.78
1Fareheit: 191.0         Celsius: 88.33
1Fareheit: 201.0         Celsius: 93.89
1Fareheit: 211.0         Celsius: 99.44
2Fareheit: 121.0         Celsius: 49.44
2Fareheit: 131.0         Celsius: 55
2Fareheit: 141.0         Celsius: 60.56
2Fareheit: 151.0         Celsius: 66.11
2Fareheit: 161.0         Celsius: 71.67
2Fareheit: 171.0         Celsius: 77.22
2Fareheit: 181.0         Celsius: 82.78
2Fareheit: 191.0         Celsius: 88.33
2Fareheit: 201.0         Celsius: 93.89
2Fareheit: 211.0         Celsius: 99.44
3Fareheit: 121.0         Celsius: 49.44
3Fareheit: 131.0         Celsius: 55
3Fareheit: 141.0         Celsius: 60.56
3Fareheit: 151.0         Celsius: 66.11
3Fareheit: 161.0         Celsius: 71.67
3Fareheit: 171.0         Celsius: 77.22
3Fareheit: 181.0         Celsius: 82.78
3Fareheit: 191.0         Celsius: 88.33
3Fareheit: 201.0         Celsius: 93.89
3Fareheit: 211.0         Celsius: 99.44
4Fareheit: 121.0         Celsius: 49.44
4Fareheit: 131.0         Celsius: 55
4Fareheit: 141.0         Celsius: 60.56
4Fareheit: 151.0         Celsius: 66.11
4Fareheit: 161.0         Celsius: 71.67
4Fareheit: 171.0         Celsius: 77.22
4Fareheit: 181.0         Celsius: 82.78
4Fareheit: 191.0         Celsius: 88.33
4Fareheit: 201.0         Celsius: 93.89
4Fareheit: 211.0         Celsius: 99.44
5Fareheit: 121.0         Celsius: 49.44
5Fareheit: 131.0         Celsius: 55
5Fareheit: 141.0         Celsius: 60.56
5Fareheit: 151.0         Celsius: 66.11
5Fareheit: 161.0         Celsius: 71.67
5Fareheit: 171.0         Celsius: 77.22
5Fareheit: 181.0         Celsius: 82.78
5Fareheit: 191.0         Celsius: 88.33
5Fareheit: 201.0         Celsius: 93.89
5Fareheit: 211.0         Celsius: 99.44
6Fareheit: 121.0         Celsius: 49.44
6Fareheit: 131.0         Celsius: 55
6Fareheit: 141.0         Celsius: 60.56
6Fareheit: 151.0         Celsius: 66.11
6Fareheit: 161.0         Celsius: 71.67
6Fareheit: 171.0         Celsius: 77.22
6Fareheit: 181.0         Celsius: 82.78
6Fareheit: 191.0         Celsius: 88.33
6Fareheit: 201.0         Celsius: 93.89
6Fareheit: 211.0         Celsius: 99.44
7Fareheit: 121.0         Celsius: 49.44
7Fareheit: 131.0         Celsius: 55
7Fareheit: 141.0         Celsius: 60.56
7Fareheit: 151.0         Celsius: 66.11
7Fareheit: 161.0         Celsius: 71.67
7Fareheit: 171.0         Celsius: 77.22
7Fareheit: 181.0         Celsius: 82.78
7Fareheit: 191.0         Celsius: 88.33
7Fareheit: 201.0         Celsius: 93.89
7Fareheit: 211.0         Celsius: 99.44
8Fareheit: 121.0         Celsius: 49.44
8Fareheit: 131.0         Celsius: 55
8Fareheit: 141.0         Celsius: 60.56
8Fareheit: 151.0         Celsius: 66.11
8Fareheit: 161.0         Celsius: 71.67
8Fareheit: 171.0         Celsius: 77.22
8Fareheit: 181.0         Celsius: 82.78
8Fareheit: 191.0         Celsius: 88.33
8Fareheit: 201.0         Celsius: 93.89
8Fareheit: 211.0         Celsius: 99.44
9Fareheit: 121.0         Celsius: 49.44
9Fareheit: 131.0         Celsius: 55
9Fareheit: 141.0         Celsius: 60.56
9Fareheit: 151.0         Celsius: 66.11
9Fareheit: 161.0         Celsius: 71.67
9Fareheit: 171.0         Celsius: 77.22
9Fareheit: 181.0         Celsius: 82.78
9Fareheit: 191.0         Celsius: 88.33
9Fareheit: 201.0         Celsius: 93.89
9Fareheit: 211.0         Celsius: 99.44
10Fareheit: 121.0        Celsius: 49.44
10Fareheit: 131.0        Celsius: 55
10Fareheit: 141.0        Celsius: 60.56
10Fareheit: 151.0        Celsius: 66.11
10Fareheit: 161.0        Celsius: 71.67
10Fareheit: 171.0        Celsius: 77.22
10Fareheit: 181.0        Celsius: 82.78
10Fareheit: 191.0        Celsius: 88.33
10Fareheit: 201.0        Celsius: 93.89
10Fareheit: 211.0        Celsius: 99.44
11Fareheit: 121.0        Celsius: 49.44
11Fareheit: 131.0        Celsius: 55
11Fareheit: 141.0        Celsius: 60.56
11Fareheit: 151.0        Celsius: 66.11
11Fareheit: 161.0        Celsius: 71.67
11Fareheit: 171.0        Celsius: 77.22
11Fareheit: 181.0        Celsius: 82.78
11Fareheit: 191.0        Celsius: 88.33
11Fareheit: 201.0        Celsius: 93.89
11Fareheit: 211.0        Celsius: 99.44
12Fareheit: 121.0        Celsius: 49.44
12Fareheit: 131.0        Celsius: 55
12Fareheit: 141.0        Celsius: 60.56
12Fareheit: 151.0        Celsius: 66.11
12Fareheit: 161.0        Celsius: 71.67
12Fareheit: 171.0        Celsius: 77.22
12Fareheit: 181.0        Celsius: 82.78
12Fareheit: 191.0        Celsius: 88.33
12Fareheit: 201.0        Celsius: 93.89
12Fareheit: 211.0        Celsius: 99.44
13Fareheit: 121.0        Celsius: 49.44
13Fareheit: 131.0        Celsius: 55
13Fareheit: 141.0        Celsius: 60.56
13Fareheit: 151.0        Celsius: 66.11
13Fareheit: 161.0        Celsius: 71.67
13Fareheit: 171.0        Celsius: 77.22
13Fareheit: 181.0        Celsius: 82.78
13Fareheit: 191.0        Celsius: 88.33
13Fareheit: 201.0        Celsius: 93.89
13Fareheit: 211.0        Celsius: 99.44
14Fareheit: 121.0        Celsius: 49.44
14Fareheit: 131.0        Celsius: 55
14Fareheit: 141.0        Celsius: 60.56
14Fareheit: 151.0        Celsius: 66.11
14Fareheit: 161.0        Celsius: 71.67
14Fareheit: 171.0        Celsius: 77.22
14Fareheit: 181.0        Celsius: 82.78
14Fareheit: 191.0        Celsius: 88.33
14Fareheit: 201.0        Celsius: 93.89
14Fareheit: 211.0        Celsius: 99.44
15Fareheit: 121.0        Celsius: 49.44
15Fareheit: 131.0        Celsius: 55
15Fareheit: 141.0        Celsius: 60.56
15Fareheit: 151.0        Celsius: 66.11
15Fareheit: 161.0        Celsius: 71.67
15Fareheit: 171.0        Celsius: 77.22
15Fareheit: 181.0        Celsius: 82.78
15Fareheit: 191.0        Celsius: 88.33
15Fareheit: 201.0        Celsius: 93.89
15Fareheit: 211.0        Celsius: 99.44
16Fareheit: 121.0        Celsius: 49.44
16Fareheit: 131.0        Celsius: 55
16Fareheit: 141.0        Celsius: 60.56
16Fareheit: 151.0        Celsius: 66.11
16Fareheit: 161.0        Celsius: 71.67
16Fareheit: 171.0        Celsius: 77.22
16Fareheit: 181.0        Celsius: 82.78
16Fareheit: 191.0        Celsius: 88.33
16Fareheit: 201.0        Celsius: 93.89
16Fareheit: 211.0        Celsius: 99.44
17Fareheit: 121.0        Celsius: 49.44
17Fareheit: 131.0        Celsius: 55
17Fareheit: 141.0        Celsius: 60.56
17Fareheit: 151.0        Celsius: 66.11
17Fareheit: 161.0        Celsius: 71.67
17Fareheit: 171.0        Celsius: 77.22
17Fareheit: 181.0        Celsius: 82.78
17Fareheit: 191.0        Celsius: 88.33
17Fareheit: 201.0        Celsius: 93.89
17Fareheit: 211.0        Celsius: 99.44
18Fareheit: 121.0        Celsius: 49.44
18Fareheit: 131.0        Celsius: 55
18Fareheit: 141.0        Celsius: 60.56
18Fareheit: 151.0        Celsius: 66.11
18Fareheit: 161.0        Celsius: 71.67
18Fareheit: 171.0        Celsius: 77.22
18Fareheit: 181.0        Celsius: 82.78
18Fareheit: 191.0        Celsius: 88.33
18Fareheit: 201.0        Celsius: 93.89
18Fareheit: 211.0        Celsius: 99.44
19Fareheit: 121.0        Celsius: 49.44
19Fareheit: 131.0        Celsius: 55
19Fareheit: 141.0        Celsius: 60.56
19Fareheit: 151.0        Celsius: 66.11
19Fareheit: 161.0        Celsius: 71.67
19Fareheit: 171.0        Celsius: 77.22
19Fareheit: 181.0        Celsius: 82.78
19Fareheit: 191.0        Celsius: 88.33
19Fareheit: 201.0        Celsius: 93.89
19Fareheit: 211.0        Celsius: 99.44
20Fareheit: 121.0        Celsius: 49.44
20Fareheit: 131.0        Celsius: 55
20Fareheit: 141.0        Celsius: 60.56
20Fareheit: 151.0        Celsius: 66.11
20Fareheit: 161.0        Celsius: 71.67
20Fareheit: 171.0        Celsius: 77.22
20Fareheit: 181.0        Celsius: 82.78
20Fareheit: 191.0        Celsius: 88.33
20Fareheit: 201.0        Celsius: 93.89
20Fareheit: 211.0        Celsius: 99.44

如果我们修改代码如下

public static void main (String[] args)
{
int Fah, i, count = 10;
int rows = 20;
double Cel, uplimit, lowlimit;
final int PER_LINE = 10;

uplimit = 212;
lowlimit = -459;

Scanner scan = new Scanner (System.in);
System.out.print("Enter the temperature in farenheit: ");
Fah = scan.nextInt();

//Gives error if you enter a number below -459 or aboce 212
while (Fah < lowlimit || Fah > uplimit)
{System.out.print("Invalid input. Please reenter: ");
Fah= scan.nextInt();
}
//Allows for max of 20 executions of the loop
int floop = Fah * 21;
double loopval = Fah;
double cloop = 1;

for ( int n = 1 ; n <= 20 ; n = n+1 )
{
// Celsius  = (Fahrenheit - 32)*5/9;
cloop = (loopval - 32)*5/9;
loopval = loopval + 10;

DecimalFormat fmt = new DecimalFormat("0.##");

System.out.println(+ n +"Fareheit: " + loopval + " t Celsius: " + fmt.format(cloop));

// Print a specific number of values per line of output
if (count % PER_LINE == 0);
}
scan.close();
}

它将生成以下结果

Enter the temperature in farenheit: 121
1Fareheit: 131.0         Celsius: 49.44
2Fareheit: 141.0         Celsius: 55
3Fareheit: 151.0         Celsius: 60.56
4Fareheit: 161.0         Celsius: 66.11
5Fareheit: 171.0         Celsius: 71.67
6Fareheit: 181.0         Celsius: 77.22
7Fareheit: 191.0         Celsius: 82.78
8Fareheit: 201.0         Celsius: 88.33
9Fareheit: 211.0         Celsius: 93.89
10Fareheit: 221.0        Celsius: 99.44
11Fareheit: 231.0        Celsius: 105
12Fareheit: 241.0        Celsius: 110.56
13Fareheit: 251.0        Celsius: 116.11
14Fareheit: 261.0        Celsius: 121.67
15Fareheit: 271.0        Celsius: 127.22
16Fareheit: 281.0        Celsius: 132.78
17Fareheit: 291.0        Celsius: 138.33
18Fareheit: 301.0        Celsius: 143.89
19Fareheit: 311.0        Celsius: 149.44
20Fareheit: 321.0        Celsius: 155

所以我对你的问题的回答是,在这种情况下,如果你考虑212的最大限制,外部for的重复将取决于用户给出的初始值。如果没有最大限制,那么代码中的这一更改将给出结果。

我已经弄清楚哪里出了问题。

import java.text.DecimalFormat;
import java.util.Scanner;
public class LaMannaAlex_Assignment2_3 
{
public static void main (String[] args)
{
int i, count;
double Fah=0, Cel=0, uplimit, lowlimit;


uplimit = 212;
lowlimit = -459;

Scanner scan = new Scanner (System.in);
System.out.print("Enter the temperature in farenheit: ");
Fah = scan.nextInt();

//Gives error if you enter a number below -459 or aboce 212
while (Fah < lowlimit || Fah > uplimit)
{System.out.print("Invalid input. Please reenter: ");
Fah= scan.nextInt();
}
//Allows for max of 20 executions of the loop
double loopval;


for (count = 0; count < 20; count++)
{
double floop = Fah;
// Celsius  = (Fahrenheit - 32)*5/9;
double cloop = (floop - 32)*5/9;

DecimalFormat fmt = new DecimalFormat("0.##");
System.out.println("Fareheit: " + Fah + " t Celsius: " + fmt.format(cloop));

Fah=Fah+10;


}


}
}

最新更新