接受put name mark、quit和get name输入的java数组(不工作)
我正在尝试写一个程序,它将接受输入“输入姓名标记”、“获取姓名标记”和“退出” 当用户输入“输入姓名标记”时,程序将提示他们输入学生姓名和标记,然后将其存储在下一个可用数组索引中 “get name”命令将接受来自用户的名称输入,它们遍历数组并显示与输入的名称匹配的任何标记 “退出”命令将结束程序并返回显示器上的平均标记和最高标记 我遇到的问题是,当我输入所需的单词时,它似乎没有进入循环。它只是跳到它再次问问题的地方,甚至不接受输入 我还是一个初学者,我已经为这个项目工作了4个星期,所以非常感谢您的帮助接受put name mark、quit和get name输入的java数组(不工作),java,arrays,Java,Arrays,我正在尝试写一个程序,它将接受输入“输入姓名标记”、“获取姓名标记”和“退出” 当用户输入“输入姓名标记”时,程序将提示他们输入学生姓名和标记,然后将其存储在下一个可用数组索引中 “get name”命令将接受来自用户的名称输入,它们遍历数组并显示与输入的名称匹配的任何标记 “退出”命令将结束程序并返回显示器上的平均标记和最高标记 我遇到的问题是,当我输入所需的单词时,它似乎没有进入循环。它只是跳到它再次问问题的地方,甚至不接受输入 我还是一个初学者,我已经为这个项目工作了4个星期,所以非常感谢
package week14;
import java.util.Scanner;
public class week {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//sets number of string inputs
{
String[] names = new String[50];
double[] scores = new double[50];
// Enter student name and score
System.out.print("please enter either: quit, put name mark, get name");
input.next();
if(input.next() == "put name mark" )
{
System.out.print("Enter Student Name");
names[50] = input.next();
System.out.print("Enter Score");
scores[50] = input.nextInt();
}
System.out.println("please enter either: quit, quit, put name mark, get name");
input.next();
if(input.next() == "get name")
{
System.out.print("please enter the name you would like to display the score for");
String get = input.next();
}
// Sort
for (int i = 50 - 1; i >= 1; i--) {
// Find the maximum in the scores[0..i]
double currentMax = scores[0];
int currentMaxIndex = 0;
for (int j = 1; j <= i; j++) {
if (currentMax < scores[j]) {
currentMax = scores[j];
currentMaxIndex = j;
}
}
// Swap scores[i] with scores[currentMaxIndex];
// Swap names[i] with names[currentMaxIndex] ;
if (currentMaxIndex != i) {
scores[currentMaxIndex] = scores[i];
scores[i] = currentMax;
String temp = names[currentMaxIndex];
names[currentMaxIndex] = names[i];
names[i] = temp;
}
if (input.equals("quit")){
System.out.print(names[i] + scores[i]);
System.out.println();
System.out.print(currentMax);
break;
}
}
}
}
}
套餐周14;
导入java.util.Scanner;
公共课周{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
//设置字符串输入的数量
{
字符串[]名称=新字符串[50];
双[]分=新双[50];
//输入学生姓名和分数
System.out.print(“请输入:退出、放置名称标记、获取名称”);
input.next();
if(input.next()=“放置名称标记”)
{
系统输出打印(“输入学生姓名”);
名称[50]=input.next();
系统输出打印(“输入分数”);
分数[50]=input.nextInt();
}
System.out.println(“请输入:quit,quit,put name mark,get name”);
input.next();
if(input.next()=“get name”)
{
System.out.print(“请输入您希望显示分数的名称”);
字符串get=input.next();
}
//分类
对于(int i=50-1;i>=1;i--){
//在分数[0..i]中找到最大值
双电流最大值=分数[0];
int currentMaxIndex=0;
对于(int j=1;j这就是我现在得到的,如果有任何问题,可能会有一些错误,说出它是什么,我会修复它
import java.util.Scanner;
public class Week
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); //Scanner used to get input from the user
String[] names = new String[50]; //The array for names remember arrays index began with 0 not 1
int[] scores = new int[50]; //Again arrays began with 0 not 1 and the last is n-1
int last = 0; //Used to store the last added ID
String command; //The command from the user
boolean running = true; //Whenever the program is running or not
while(running)
{
System.out.println("please enter either: quit, put name mark, get name"); //Print the command list
command = input.nextLine(); //The next input line (This will make the Thread pause untill it get and input)
if(command.equals("put mark")) //If the command is "put mark"
{
if(last == 49) //Check because we can create and Exception by adding too much element to and array
System.out.println("Max number of people reached"); //So we can't add more people
else
{
System.out.println("Enter Student Name"); //Print the questin
names[last] = input.nextLine(); //The name
System.out.println("Enter Score"); //Ask for the score
scores[last] = input.nextInt(); //Get the score ,because score is a double we should use double so it can take numbers like 0.1
last++; //Increment last with 1
}
}else if(command.equals("get name"))
{
System.out.println("please enter the name you would like to display the score for");
String name = input.nextLine(); //Get the name
for(int i = 0; i < last; i++) //Loop untill we hit the last added name's ID
if(names[i].equals(name)) //Check if the names[i] is the name that we're searching for
System.out.println(name + " 's score is " + scores[i]); //If it's then we print it out
}else if(command.equals("quit"))
{
running = false; //The loop will never run again
//Implement sorting for youself I would use Map<K, V> but you didn't learned it so..
//In this case you have to make 1 loop to sort both of the arrays by sorting the second array
//and when you move anything must it in both arrays I can't help you to make this sorry
for(int i = 0; i < last; i++) //We print the sorted arrays of the people and their scores
System.out.println(names[i] + " 's score is " + scores[i]); //Let's print it
}
}
}
}
import java.util.Scanner;
公共课周
{
公共静态void main(字符串[]args)
{
扫描器输入=新扫描器(System.in);//用于从用户获取输入的扫描器
String[]names=new String[50];//名称数组memory arrays索引以0而不是1开头
int[]scores=new int[50];//数组再次以0而不是1开头,最后一个是n-1
int last=0;//用于存储上次添加的ID
String command;//来自用户的命令
boolean running=true;//无论程序是否运行
(跑步时)
{
System.out.println(“请输入:quit、put name mark、get name”);//打印命令列表
command=input.nextLine();//下一个输入行(这将使线程暂停,直到获取并输入为止)
if(command.equals(“put mark”)//如果命令是“put mark”
{
if(last==49)//检查,因为我们可以通过向和数组添加太多元素来创建和异常
System.out.println(“达到的最大人数”);//因此我们无法添加更多的人
其他的
{
System.out.println(“输入学生姓名”);//打印问题
names[last]=input.nextLine();//名称
System.out.println(“输入分数”);//询问分数
scores[last]=input.nextInt();//获取分数,因为分数是双精度的,所以我们应该使用双精度,这样它可以接受像0.1这样的数字
last++;//用1递增last
}
}else if(command.equals(“get name”))
{
System.out.println(“请输入要显示分数的名称”);
String name=input.nextLine();//获取名称
for(int i=0;i
First的可能重复项您不能将字符串与==进行比较,必须使用.equals(object)然后再使用input.next()在问题之后,这使得第一个如果没有用处的话,那么“names[50]=input.next();”将不起作用,它将只设置名称中的第51个(数组以索引0开头,因此第50个元素是“names[49]”)。你知道函数吗?我还不知道函数,但我会马上开始看在线教程。所以我应该将数组设置为0?到这里来。我会为你重写它,并且我会尽量让你容易理解。哇,非常感谢你的帮助!!!!!我真的太感谢你了。我把它放到ecli时出错了pse.在分数[最后]部分说它必须是数组类型,但它解决了double忘记将[]添加到double抱歉。我添加了它。如果您有任何进一步的帮助,请询问。出于某种原因,当我为put name mark输入学生名时,它会给出一条错误消息,而不是要求我输入分数。否则如果(command.equals(quit))
应该是else