gpt4 book ai didi

java - 如何在Java中循环一个简单的程序?

转载 作者:行者123 更新时间:2023-11-30 07:27:29 26 4
gpt4 key购买 nike

我正在尝试编写一个简单的程序,用户可以在其中查看和更新​​ NBA 球员争夺 MVP 奖杯的列表。然而,我过去未能编写一个程序,其中可以循环用户决定的时间。我希望程序有选项 1. 返回 & 2. 退出,但我不知道如何循环它。这是我的 Rank.java 和 AdminAccount.java。希望不会造成理解困惑,感谢您的阅读。

import java.util.Scanner;

public class Rank {
String player[] = { "Stephen Curry", "Russel Westbrook", "Kevind Durant", "LeBron James", "Kawhi Leonard" };
Scanner rankInput = new Scanner(System.in);
Scanner playerInput = new Scanner(System.in);
int rank;
String playerUpdate;

public void Rank() {
System.out.println("Rank\tPlayer");
for (int counter = 0; counter < player.length; counter++) {
System.out.println(counter + 1 + "\t" + player[counter]);
}
}

public void updateRank() {
System.out.print("Select rank to update: ");
rank = rankInput.nextInt();

if (rank == 1) {
System.out.print("\nPlayer Name: ");
playerUpdate = playerInput.nextLine();
player[0] = playerUpdate;
} else if (rank == 2) {
System.out.print("\nPlayer Name: ");
playerUpdate = playerInput.nextLine();
player[1] = playerUpdate;
} else if (rank == 3) {
System.out.print("\nPlayer Name: ");
playerUpdate = playerInput.nextLine();
player[2] = playerUpdate;
} else if (rank == 4) {
System.out.print("\nPlayer Name: ");
playerUpdate = playerInput.nextLine();
player[3] = playerUpdate;
} else if (rank == 5) {
System.out.print("\nPlayer Name: ");
playerUpdate = playerInput.nextLine();
player[4] = playerUpdate;
}
}
}

import java.util.Scanner;

public class AdminAccount {

public static void main(String[] args) {
Rank rank = new Rank();

Scanner adminInput = new Scanner(System.in);
Scanner exitInput = new Scanner(System.in);

boolean keepRunning = true;
// menu variables
int menuOption;
int exitOption;

while (keepRunning) {
System.out.println("*** NBA MVP Race Administor Account ***");
System.out.print("\n1.Ranking 2.Update\t- ");
menuOption = adminInput.nextInt();
System.out.println("");

if (menuOption == 1) {
rank.Rank();
} else if (menuOption == 2) {
rank.updateRank();
}
}
}
}

最佳答案

只需在循环中添加一个“退出”选项即可:

while(keepRunning){
System.out.println("*** NBA MVP Race Administor Account ***");
System.out.print("\n1.Ranking 2.Update 3.Exit\t- ");
menuOption = adminInput.nextInt();
System.out.println("");

if(menuOption == 1)
{
rank.Rank();
}
else if(menuOption == 2)
{
rank.updateRank();
}
else
{
keepRunning = false;
}
}

关于java - 如何在Java中循环一个简单的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36625540/

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