编写此应用程序的更好方法是什么?
What is a better way to program this application?
我正在为我的 class 做作业,我正在尝试显示如下图所示的内容:
问题是,我是 java 的新手,我不知道如何在不对每个循环都进行硬编码的情况下使这样的嵌套循环工作。我的问题是......我怎样才能使这段代码更高效、更动态?
进口java.util.Scanner;
public class 条形图 {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int score1;
int score2;
int score3;
int score4;
int score5;
final String PROMPT = "Enter points scored by ";
System.out.print(PROMPT + " Art >>>");
score1 = scn.nextInt();
System.out.print(PROMPT + " Bob >>>");
score2 = scn.nextInt();
System.out.print(PROMPT + " Cal >>>");
score3 = scn.nextInt();
System.out.print(PROMPT + " Dan >>>");
score4 = scn.nextInt();
System.out.print(PROMPT + " Eli >>>");
score5 = scn.nextInt();
System.out.print("Art ");
for (int y = 1; y <= score1; y++)
{
System.out.print(" *");
}
System.out.print("\n");
System.out.print("Bob ");
for (int y = 1; y <= score2; y++)
{
System.out.print(" *");
}
System.out.print("\n");
System.out.print("Cal ");
for (int y = 1; y <= score3; y++)
{
System.out.print(" *");
}
System.out.print("\n");
System.out.print("Dan ");
for (int y = 1; y <= score4; y++)
{
System.out.print(" *");
}
System.out.print("\n");
System.out.print("Eli ");
for (int y = 1; y <= score5; y++)
{
System.out.print(" *");
}
}
}
创建一个包含地图的列表,列表将保存用户条目,地图将包含名称(键)和点(值)。
您可以将所有姓名和分数存储到数组中。然后你可以使用for循环来遍历数组:
Scanner scn = new Scanner(System.in);
int[] scores = new int[] {0,0,0,0,0};
String[] names = new String[] {"Art", "Bob", "Cal", "Dan", "Eli"};
final String PROMPT = "Enter points scored by ";
// a loop to ask for input
// you can treat these loops as saying "for each name in the names array, do this..."
for(int i = 0 ; i < names.length ; i++) {
// in the first iteration "names[i]" will be "Art", second iteration
// will be "Bob", and so on
System.out.print(PROMPT + names[i] + " >>>");
// set the corresponding score
scores[i] = scn.nextInt();
}
// another loop to print a bar chart
for(int i = 0 ; i < names.length ; i++) {
System.out.print(names[i] + " ");
for (int y = 1; y <= scores[i]; y++) {
System.out.print(" *");
}
System.out.print("\n");
}
我正在为我的 class 做作业,我正在尝试显示如下图所示的内容:
问题是,我是 java 的新手,我不知道如何在不对每个循环都进行硬编码的情况下使这样的嵌套循环工作。我的问题是......我怎样才能使这段代码更高效、更动态?
进口java.util.Scanner; public class 条形图 {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int score1;
int score2;
int score3;
int score4;
int score5;
final String PROMPT = "Enter points scored by ";
System.out.print(PROMPT + " Art >>>");
score1 = scn.nextInt();
System.out.print(PROMPT + " Bob >>>");
score2 = scn.nextInt();
System.out.print(PROMPT + " Cal >>>");
score3 = scn.nextInt();
System.out.print(PROMPT + " Dan >>>");
score4 = scn.nextInt();
System.out.print(PROMPT + " Eli >>>");
score5 = scn.nextInt();
System.out.print("Art ");
for (int y = 1; y <= score1; y++)
{
System.out.print(" *");
}
System.out.print("\n");
System.out.print("Bob ");
for (int y = 1; y <= score2; y++)
{
System.out.print(" *");
}
System.out.print("\n");
System.out.print("Cal ");
for (int y = 1; y <= score3; y++)
{
System.out.print(" *");
}
System.out.print("\n");
System.out.print("Dan ");
for (int y = 1; y <= score4; y++)
{
System.out.print(" *");
}
System.out.print("\n");
System.out.print("Eli ");
for (int y = 1; y <= score5; y++)
{
System.out.print(" *");
}
}
}
创建一个包含地图的列表,列表将保存用户条目,地图将包含名称(键)和点(值)。
您可以将所有姓名和分数存储到数组中。然后你可以使用for循环来遍历数组:
Scanner scn = new Scanner(System.in);
int[] scores = new int[] {0,0,0,0,0};
String[] names = new String[] {"Art", "Bob", "Cal", "Dan", "Eli"};
final String PROMPT = "Enter points scored by ";
// a loop to ask for input
// you can treat these loops as saying "for each name in the names array, do this..."
for(int i = 0 ; i < names.length ; i++) {
// in the first iteration "names[i]" will be "Art", second iteration
// will be "Bob", and so on
System.out.print(PROMPT + names[i] + " >>>");
// set the corresponding score
scores[i] = scn.nextInt();
}
// another loop to print a bar chart
for(int i = 0 ; i < names.length ; i++) {
System.out.print(names[i] + " ");
for (int y = 1; y <= scores[i]; y++) {
System.out.print(" *");
}
System.out.print("\n");
}