java - 字符串 - 全局设置

标签 java string global-variables globalization

好吧,我创建了这个未完成的程序,它允许您将学生姓名和测试分数插入到字符串中。但我的可能是,我尝试使用 if 语句,以便用户能够输入数字并选择他们想要执行的操作。

我试图让用户插入姓名和测试分数后,他们可以选择打印姓名和测试分数,或者他们可以退出或插入另一个名称,但我不能,因为未设置字符串全局范围内。

有人知道怎么做吗?谢谢您,

package week14;

import java.util.Scanner;

public class Task4 {
    private static Scanner input;
        private static Scanner input2;

        public static void main (String [] args){
            input2 = new Scanner (System.in);

            System.out.println("Student Mark Program");
            System.out.println("\nHere are your options:");
            System.out.println("\n1. Insert Student name and mark");
            System.out.println("2. Quit");

        int choice = input2.nextInt();
            System.out.println();

                if (choice == 1){
                    opt1();
                }
                else if (choice == 2){
                    quit();
                }
        }

                public static void opt1(){

                input = new Scanner(System.in);
                    String firstname;
                        String surname;

                int score1;
                System.out.print("Enter the students first name: ");
                firstname = input.next();

                System.out.print("Enter the students surname: ");
                surname = input.next();

                String full_name;
                full_name = firstname + " " + surname;

                System.out.println("Please enter students score: ");
                score1 = input.nextInt();

                System.out.println("Name : " + full_name);
                System.out.println("Score: " + score1);

                System.out.println();
                System.out.println("\nHere are your options:");
                System.out.println("\n1. Insert Another Student name");
                System.out.println("2. Get student name");
                System.out.println("3. Get student name and mark");
                System.out.println("4. Quit");

                int choice = input.nextInt();
                System.out.println();

                if (choice == 1){
                    opt6();
                }
                else if (choice == 2){
                    opt2();
                }
                else if (choice == 3){
                    opt3();
                }

                System.out.println();
                input.close();
                }



                public static void opt2(){

                System.out.println("Name : " + full_name);

                }

                public static void opt3(){


                }

                public static void opt4(){

                }

                public static void opt5 (){

                }

                public static void opt6 (){

                }

                public static void quit(){
                    System.out.println("You have quit the program");
                    System.exit(0);
                    }
                }

最佳答案

将此 static String full_name; 放在类中声明 private static Sc​​anner input2; 的位置下。使用全局变量不一定是良好的编程习惯,但出于您的目的,它可能会完成工作。

关于java - 字符串 - 全局设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28709266/

相关文章:

java - 声明后编译器无法找到 RandomAccessFile

arrays - 如何在另一个字符串的开头匹配数组中的字符串

c - extern const 数组声明/初始化错误(无效的初始化器/冲突类型)

java - 如何使用 SSHJ 保持 SSH 连接?

java - 即使有足够的 Java 内存,也会重复进行 Java 垃圾回收 - 为什么

python 中的 java 的 printStackTrace() 等价物

c - 76 个字符长的字符串的单义哈希函数

c# - 在C#中使用字符串列表作为参数格式化字符串

php - 如何从同一类的不同函数访问变量?

variables - 如何从 GM_xmlhttprequest 返回值?