创建 do-while 循环以提示用户是否要输入另一个 ISBN
Creating do-while loop to prompt user if they want to enter another ISBN
因此,作为我的 class 的一个项目,我需要制作一个程序,让用户输入 ISBN 编号的前 n 位数字并以第十位数字作为响应。我一直在无休止地寻找可以让我的程序询问用户是否要再次 运行 程序然后在他们输入 'yes' 或 [= 时再次 运行 程序的东西14=]。我发现什么都没有用,通常它只是循环“你想输入另一个 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);
}
}
}
尝试这样的事情(我已经更改了 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");
}
因此,作为我的 class 的一个项目,我需要制作一个程序,让用户输入 ISBN 编号的前 n 位数字并以第十位数字作为响应。我一直在无休止地寻找可以让我的程序询问用户是否要再次 运行 程序然后在他们输入 'yes' 或 [= 时再次 运行 程序的东西14=]。我发现什么都没有用,通常它只是循环“你想输入另一个 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);
}
}
}
尝试这样的事情(我已经更改了 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");
}