gpt4 book ai didi

java - 创建 do-while 循环来提示用户是否要输入另一个 ISBN

转载 作者:行者123 更新时间:2023-12-01 11:53:53 25 4
gpt4 key购买 nike

因此,作为我的类(class)项目,我需要编写一个程序,让用户输入 ISBN 号码的前 n 位数字,并以第十位数字进行响应。我一直在无休止地寻找一些东西,让我的程序询问用户是否要再次运行该程序,然后在用户输入"is"或“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);
}
}
}

最佳答案

尝试这样的事情(我已经更改了 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");
}

关于java - 创建 do-while 循环来提示用户是否要输入另一个 ISBN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28587227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com