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
使用Java的数字和_Java_Loops_For Loop_If Statement_While Loop - Fatal编程技术网

使用Java的数字和

使用Java的数字和,java,loops,for-loop,if-statement,while-loop,Java,Loops,For Loop,If Statement,While Loop,我是一名编程初学者,我正在尝试使用Java输出用户输入的数字总和 代码应该根据数字的长度显示特定消息,并在用户键入空格时结束 我的代码执行正确。但是,它只对第一个输入有效。在第一次输入之后,我收到一条错误消息 如何修复代码,使while循环高效地工作 //scanner Scanner input = new Scanner (System.in); //ask user for a credit card number System.out.print(&qu

我是一名编程初学者,我正在尝试使用Java输出用户输入的数字总和

代码应该根据数字的长度显示特定消息,并在用户键入空格时结束

我的代码执行正确。但是,它只对第一个输入有效。在第一次输入之后,我收到一条错误消息


如何修复代码,使while循环高效地工作

    //scanner
    Scanner input = new Scanner (System.in);

    //ask user for a credit card number
    System.out.print("Enter a credit card number (enter a blank line to quit): ");
    String userNum = input.nextLine();
    
    
    //length and last char of string
    int len = userNum.length();
    char lastDigit = userNum.charAt(len-1);
    
    
    //initial values
    int sumOfDigits = 0;
    int strNum = 0;
    int i;

    //loops
    while (len > 0) {
        if (len == 16) { //when length equals 16
            for (i = 0; i < 15; ++i ) { //calculates sum of digits
                String s = userNum.substring(i, i+1);
                strNum = Integer.parseInt(s);
                sumOfDigits = sumOfDigits + strNum;
            }
            System.out.println("DEBUG: Sum is " + sumOfDigits);
            System.out.println("Check digit is: " + lastDigit);
            System.out.println();    
            System.out.print("Enter a credit card number (enter a blank line to quit): ");
            userNum = input.next();
        }
        else if (userNum.equals("")) { //when user types blank space
            System.out.println("Goodbye!");
        }
        else { //when user digit is not 16, nor types a blank space
            System.out.println("ERROR! Number MUST have exactly 16 digits."); 
            System.out.println();
            System.out.print("Enter a credit card number (enter a blank line to quit): ");
                userNum = input.next();
        }
    }
    
    
    
    //input close
    input.close();

}
//扫描仪
扫描仪输入=新扫描仪(System.in);
//向用户询问信用卡号码
系统输出打印(“输入信用卡号(输入空行退出):”;
字符串userNum=input.nextLine();
//字符串的长度和最后一个字符
int len=userNum.length();
char lastDigit=userNum.charAt(len-1);
//初始值
int-sumOfDigits=0;
int strNum=0;
int i;
//循环
而(len>0){
当长度等于16时,如果(len==16){//
对于(i=0;i<15;++i){//计算数字之和
字符串s=userNum.substring(i,i+1);
strNum=Integer.parseInt(s);
sumOfDigits=sumOfDigits+strNum;
}
System.out.println(“调试:总和为”+sumOfDigits);
System.out.println(“校验位为:“+lastDigit”);
System.out.println();
系统输出打印(“输入信用卡号(输入空行退出):”;
userNum=input.next();
}
当用户键入空格时,如果(userNum.equals(“”){//else
System.out.println(“再见!”);
}
else{//当用户数字不是16时,也不键入空格
System.out.println(“错误!数字必须正好有16位。”);
System.out.println();
系统输出打印(“输入信用卡号(输入空行退出):”;
userNum=input.next();
}
}
//输入关闭
input.close();
}

我看到的主要问题是,您没有从新输入的字符串中重新计算内容。我猜具体的问题是,
len
没有被重新计算,这意味着在每次循环迭代时,您将在条件语句中输入相同的分支

将输入内容的所有读数放在一个位置,并将所有变量移动到循环中:

while (true) {
  System.out.print("Enter a credit card number (enter a blank line to quit): ");
  // Read it.
  String userNum = input.nextLine();

  if (userNum.equals("")) {
    System.out.println("Goodbye!");
    break;
  }

  if (len == 16) {

    // Derive the values you want from it.
    //length and last char of string
    int len = userNum.length();
    char lastDigit = userNum.charAt(len-1);

    // ..
  } else {
    // Print an error... but allow the loop to execute again.
  }
}

这样,您就不必担心忘记重新初始化变量,或者确保打印的消息与输入另一个值相同,也不必担心读取输入的方式不同(例如,您的代码既包含
input.next
又包含
input.nextLine

“这样我的while循环就能有效地工作“如果你的代码不起作用,在考虑效率之前先处理好它。在我的电脑上,你的代码可以处理几个相继输入的信用风险值。您看到了哪条错误消息?我看到的唯一问题是您的程序没有说再见和终止。在阅读新值后,您需要重新计算
len
lastDigit
。为了更好地接收您的问题(更少的否决票,更多更好的答案),请始终清楚准确地说明您的计划的预期结果以及观察结果的差异。粘贴问题中出现的任何错误消息。