帮助Java中的while循环. [英] Help with a while loop in java.

查看:52
本文介绍了帮助Java中的while循环.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用以下代码时遇到麻烦.该程序的功能是根据钻石的重量确定钻石的等级(如其珍贵程度).到目前为止,我的工作方式一直允许我输入钻石的重量.因为将会有很多钻石,但是我没有使用固定数量的数组.当用户输入-999时,程序应退出.该数组总共有10个插槽,但是我只是想对每个级别的"A-F"钻石进行测试,因此在6个之后,我想输入-999退出程序.

I''m having trouble with the following code. This program''s function is to determine the class (class as in how precious it is) of a diamond depending on its weight. So far I have got it working in a way that it keeps allowing me to enter the weight of the diamonds. Because there are going to be numerous diamonds, but no set amount I have used arrays. The program should exit when the user enters -999. The array has a total of 10 slots, but I''m only looking to test it for each class of diamond "A-F" so after 6 I want to enter -999 to exit the program.

import java.util.*;
public class forDiamond {
	
	public static void main(String[] args)
	{
	
		//Declaring variables and arrays.
		
		int[] weight;;
		char[] diamondClass;
		int[] diamond;
		weight = new int[10];
		diamond = new int [10];
		diamondClass = new char [10];
		Scanner in = new Scanner(System.in);
		
		//Getting the dweight to determine weather the while loop is true or false
		System.out.println("Please enter the weight of the Diamond: ");
		int dweight = in.nextInt();
		while (dweight != -999)
		{
			for(int i=0; i<10; i++)
			{
				weight[i] = dweight;
				diamond[i] = i+1;
				//finding out the diamond class
				if (weight[i] <=15)
					diamondClass[i] = 'F';
				
				else if (weight[i] > 15 && weight[i]<=40)
					diamondClass[i] = 'E';
				
				else if (weight[i] > 40 && weight[i]<=60)
					diamondClass[i] = 'D';
				
				else if (weight[i] > 60 && weight[i]<=80)
					diamondClass[i] = 'C';
				
				else if (weight[i] > 80 && weight[i]<=100)
					diamondClass[i] = 'B';
				
				else if (weight[i] > 100)
					diamondClass[i] = 'A';
				
				System.out.println("Please enter the weight of the Diamond: ");
				dweight = in.nextInt();
			}
			

		}
		//Printing out all the results
		for(int i=0; i>10; i++) 
		System.out.println("The weight of " + diamond[i] + " is: " + weight[i] + "the class of the diamond is :" + diamondClass[i]);

	}

}

推荐答案

System.out.println("Please enter the weight of the Diamond: ");
dweight = in.nextInt();



这应该是您在for循环之外的while循环中要做的最后一件事.当前,如果在for循环执行10次迭代之前输入-999,则将不会退出应用程序,因为在内部循环完成之前不会再次检查while条件.

也许您可以使用ArrayLists而不是Arrays,并在while循环中的每次迭代中将值添加到ArrayList的末尾.然后,您无需提前定义大小,并且您可以在用户输入值时进行任意多次迭代.

希望对您有所帮助.



This needs to be the last thing you do in the while loop, outside of the for loop. Currently if you enter -999 before the for loop has done 10 iterations it won''t exit the application as the while condition is not checked again until the inner loop is complete.

Maybe you could use ArrayLists rather than Arrays and add the values to the end of the ArrayList each iteration in the while loop. Then you don''t need to define the size ahead of time and you can do as many iterations as the user enters values.

Hope this helps.


如果我找到您,请从
进行更改
If I got you then changing from
for(int i=0; i<10; i++)




to

for(int i=0; i<10 && dweight != -999; i++)


应该就足够了(这种方式,但是不需要while循环).


should be enough (this way, however the while loop is not needed).


这篇关于帮助Java中的while循环.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆