Java 对于循环错误->;if语句不在中生成输出
这是一些工作,我更希望自己弄清楚,所以请你不要马上给出答案,如果可能的话,但也许可以给我指出正确的方向,为什么我从循环输出接收错误,为什么if语句不产生输出 我必须允许用户输入“建议的最高员工工资”,然后从不同店铺单位的文件中读取文本,以计算每个店铺单位的员工工资和总员工成本 这是我要读的课文Java 对于循环错误->;if语句不在中生成输出,java,loops,if-statement,Java,Loops,If Statement,这是一些工作,我更希望自己弄清楚,所以请你不要马上给出答案,如果可能的话,但也许可以给我指出正确的方向,为什么我从循环输出接收错误,为什么if语句不产生输出 我必须允许用户输入“建议的最高员工工资”,然后从不同店铺单位的文件中读取文本,以计算每个店铺单位的员工工资和总员工成本 这是我要读的课文 Unit One 4 32 8 38 6 38 6 16 7 Unit Two 0 Unit Three 2 36 7 36 7 Unit Four 6 32 6.5 32 6.5 36 6.5 3
Unit One
4
32 8
38 6
38 6
16 7
Unit Two
0
Unit Three
2
36 7
36 7
Unit Four
6
32 6.5
32 6.5
36 6.5
36 6.5
38 6.5
38 6.5
...continued up to 9 shop units.
这是我的代码:
public class Recursion {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count =0;
int n = 1;
int t=0;
int triangularNumber =0;
while (n<Integer.MAX_VALUE)
{
t = isTriangularNumber(n,count,triangularNumber);
triangularNumber=0;
int starNumber= ((6*n)*(n-1)) + 1;
if (starNumber ==t)
{
System.out.println(t);
}
n++;
}
if (n==Integer.MAX_VALUE)
{
System.exit(0);
}
}
public static int isTriangularNumber(int n, int count, int triangularNumber)
{
triangularNumber =triangularNumber + (n-(n-count));
if (count<=n)
{
return isTriangularNumber(n,(count++), triangularNumber);
}
else return triangularNumber;
}
你知道这是什么原因吗 if语句没有输出的原因是该语句在循环之外。正确格式化代码应该有助于您更早地识别此类错误
结束(int b=0;b的
的大括号以下几行给出了一个错误:
Unitnum = inFile.nextLine();
Unitnum = inFile.nextLine();
您可以将它们包装成:
if (b < shopunits - 1) {
...
}
if(b
您已到达文件的末尾,扫描仪在导致此问题的文件末尾尝试读取下一行。Adarsh说的很可能是正确的,您应该做的是将其包装在while循环中
while(infile.hasNextLine()){
...
}
这样,当它结束的时候,你就不会失败。
我检查并编辑了你的代码的格式,一开始很难理解。我想你的if块的问题是上面没有括号
试试这个:
if(total > recommended_max){
System.out.println("Higher");
}else{
System.out.println("Lower");
}
我知道它有时不带括号也能用,但有时取决于它是怎么写的或写在哪里的,所以放上括号总是好的
对于末尾的错误,下面For循环末尾的部分似乎在每次迭代后尝试读取行:
Unitnum=infle.nextLine();
Unitnum=infle.nextLine()
因此,要么在文件末尾有两行空行,要么在循环的开头读一行:
while(inFile.hasNextLine()) {
Unitnum = inFile.nextLine();
saleassist = inFile.nextInt();
.......
}
关于“if”块的第二个问题与每次迭代结束时将total的值设置为0有关:
小时=0;
比率=0;
总计=0;
您需要在外部循环之外声明额外的变量,以保存循环之间的总计
double sumOfTotal=0;
......
System.out.println("The total staff cost of " + Unitnum +" is £"+ total);
//this holds the sum of totals across loops
sumOfTotal += total;
......
total=0;
}
if (sumOfTotal > reccomended_max)
System.out.println("Higher");
else
System.out.println("Lower");
粗略猜测:您的两个infle.nextLine()
调用试图运行到输入文件的末尾?好的,谢谢,只是想知道我将在何处添加代码行?我现在使用if语句工作正常,只是循环有问题still@user2863681您有两行连续的Unitnum=infle.nextLine()
在那里。当前,它们总是在执行。您可以在它们周围添加if(b!=shopunits-1)
,或者在每一行前面添加if(!infle.hasNextLine())break;
。这两种方法都可以。
if(total > recommended_max){
System.out.println("Higher");
}else{
System.out.println("Lower");
}
while(inFile.hasNextLine()) {
Unitnum = inFile.nextLine();
saleassist = inFile.nextInt();
.......
}
double sumOfTotal=0;
......
System.out.println("The total staff cost of " + Unitnum +" is £"+ total);
//this holds the sum of totals across loops
sumOfTotal += total;
......
total=0;
}
if (sumOfTotal > reccomended_max)
System.out.println("Higher");
else
System.out.println("Lower");