gpt4 book ai didi

java - 扫描字符串然后将它们与整数一起打印

转载 作者:行者123 更新时间:2023-12-02 04:29:34 24 4
gpt4 key购买 nike

如果输入是曼迪 99 99 97 95 92 90 98并且输出需要是曼迪A到目前为止我的代码是:

package utility;
import java.util.Scanner;
public class LetterGrader {



public static void main(String[] args) {
String Name = new String();
int quiz1;
int quiz2;
int quiz3;
int quiz4;
int mid1;
int mid2;
int Final;
double grade;
Scanner getScores = new Scanner (System.in);
Scanner getName = new Scanner(System.
System.out.println("Please enter name with test scores with a space in between each, in the order of "
+ "Quiz 1 Quiz 2 Quiz 3 Quiz 4\n Midterm 1 Midterm 2 and Final\n");//Shows the format needed
Scanner scanner = new Scanner(System.in);
Name = getName.next();
quiz1 = getScores.nextInt();
quiz2 = getScores.nextInt();
quiz3 = getScores.nextInt();
quiz4 = getScores.nextInt();
mid1 = getScores.nextInt();
mid2 = getScores.nextInt();
Final = getScores.nextInt();

grade =( quiz1 * .10 + quiz2 * .10 + quiz3 * .10 + quiz4 * .10
+ mid1 * .20 + mid2 * .15 + Final * .25);
System.out.print(Name);
if (grade >= 90)
System.out.println("A");
if (grade >= 80 && grade< 90)
System.out.println("B");
if (grade >= 70 && grade< 80)
System.out.println("C");
if (grade >= 60 && grade< 70)
System.out.println("D");
if (grade <= 59)
System.out.println("F");

}}

我如何扫描用户输入的名称,然后用数字打印它?任何帮助都很棒。

最佳答案

您应该阅读整行并将其分成几部分。

public static void main(String[] args) {
String Name = new String();
int quiz1;
int quiz2;
int quiz3;
int quiz4;
int mid1;
int mid2;
int Final;
double grade;

Scanner getScores = new Scanner(System.in);
System.out.println("Please enter name with test scores with a space in between each, in the order of "
+ "Quiz 1 Quiz 2 Quiz 3 Quiz 4\n Midterm 1 Midterm 2 and Final\n");//Shows the format needed
String[] inputs = getScores.readLine ().Split (" ");
Name = inputs[0]
quiz1 = inputs[1]
quiz2 = inputs[2]
quiz3 = inputs[3]
quiz4 = inputs[4]
mid1 = inputs[5]
mid2 = inputs[6]
Final = inputs[7]

grade =( quiz1 * .10 + quiz2 * .10 + quiz3 * .10 + quiz4 * .10
+ mid1 * .20 + mid2 * .15 + Final * .25);
System.out.print(Name);
if (grade >= 90)
System.out.println("A");
if (grade >= 80 && grade< 90)
System.out.println("B");
if (grade >= 70 && grade< 80)
System.out.println("C");
if (grade >= 60 && grade< 70)
System.out.println("D");
if (grade <= 59)
System.out.println("F");

getScores.close();

}

关于java - 扫描字符串然后将它们与整数一起打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31645381/

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