因此,作为我班级的项目,我需要制作一个程序,让用户输入ISBN编号的前n位数字,并以第十位数字作出响应。我无休止地搜索了一些东西,它允许我的程序询问用户是否要再次运行程序,然后让它再次运行程序,如果他们输入“是”或“Y”。我发现的东西没有用,通常它最终会循环“你想输入另一个ISBN而不实际让他们输入ISBN。我的代码在下面,谢谢你们的任何帮助!
import java.util.Scanner;
public class ISBNCheckSum {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System. in );
// limiter
int isbn10 = 9;
// to take in response of user
long userResponse;
// accumulator
int ISBnNum = 1;
//current count of ISBN
long isbnCount = 0;
// This is used to multiply the userresponse by 1,2,3... up to 9
int multiplier = 1;
while (ISBnNum <= isbn10)
{
System.out.println("Please ISBN number " + ISBnNum);
//to enter the User response
userResponse = keyboard.nextInt();
//Multiply the user response by multiplier variable
userResponse = userResponse * multiplier;
//add to accumulator
ISBnNum = ISBnNum + 1;
// put user into final answer
isbnCount = isbnCount + userResponse;
// increase multiplier
multiplier = multiplier + 1;
}
long checkSum;
checkSum = isbnCount % 11;
System.out.println(checkSum);
}
}
}
答案 0 :(得分:1)
尝试这样的事情(我已经更改了iSBnNum变量名称):
boolean askAgain = true;
while (askAgain) {
iSBnNum = 1;
while (iSBnNum <= isbn10) {
System.out.println("Please ISBN number " + iSBnNum);
// to enter the User response
userResponse = keyboard.nextInt();
// Multiply the user response by multiplier variable
userResponse = userResponse * multiplier;
// add to accumulator
iSBnNum = iSBnNum + 1;
// put user into final answer
isbnCount = isbnCount + userResponse;
// increase multiplier
multiplier = multiplier + 1;
}
System.out.println("Ask again (Y/N)?");
String answer = keyboard.next();
askAgain = answer.equalsIgnoreCase("Y");
}