【问题标题】:Strings - setting it globally字符串 - 全局设置
【发布时间】:2015-02-25 00:54:23
【问题描述】:

好的,所以我创建了这个未完成的程序,它允许您将学生姓名和考试成绩插入到字符串中。但我可能是,我试图使用 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);
                    }
                }

【问题讨论】:

    标签: java string global-variables globalization


    【解决方案1】:

    把这个static String full_name; 放在你在课堂上声明private static Scanner input2; 的地方。使用全局变量不一定是良好的编程习惯,但出于您的目的,它可能会完成工作。

    【讨论】:

      【解决方案2】:

      好吧,我将初始化一个新的全局变量,将您的 full_name 设置为并将其作为访问器方法返回。

      public static String getName()
      {
          return (nameOfString);
      }
      

      另外,我看到您想要执行其他命令来更改您的姓名,您可以使用 settor 方法来做到这一点。

      public static void setName(String newName)
      {
          originalString = newName;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 2018-01-09
        • 1970-01-01
        相关资源
        最近更新 更多