变量未在不同情况下初始化

发布于 2025-01-24 21:13:12 字数 5848 浏览 2 评论 0原文

//(old)你好,所以我正在编写一个程序,要求用户提供一个选项并运行为每个方法创建的方法 有2个输入文件“ input1.txt”和“ input2.txt”,但我不会在这里包含它们,因为它们占用了太多空间,这与问题无关。因此,到目前为止,我只完成了选项2的工作,但是当我在hackerank上对其进行测试时,有些不对劲。

(new)以下是我的代码(未完成代码)

更新:您好,下面我会发布完整的修订代码,

import static java.lang.System.*;
import java.io.FileNotFoundException;
import java.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.ArrayList;

public class Main
{
  public static void main (String[]args) throws IOException
  {
    PrintWriter pw1 = new PrintWriter ("input.txt");
      pw1.println ("Mickey Mouse CS 98.7 67.8 23.5");
      pw1.println ("Minnie Mouse ENG 45.6 98.3 94.7");
      pw1.println ("Donald Duck NET 56.8 74.2 78.4");
      pw1.println ("Bob Builder CS 78.5 89.4 82.5");
      pw1.println ("Snow White MAT 56.6 32.4 56.6");
      pw1.println ("Hellen Keller CHEM 78.8 23.1 99.6");
      pw1.println ("Daffy Duck ENG 67.4 55.5 89.5");
      pw1.println ("Fred Flinstone MAT 45.3 87.4 38.9");
      pw1.println ("Daffy Duck CS 76.5 22.2 88.5");
      pw1.println ("Bugs Bunny NET 68.4 89.7 95.6");
      pw1.println ("Winnie Pooh CHEM 77.5 89.4 98.2");
      pw1.close ();
    PrintWriter pw2 = new PrintWriter ("input2.txt");
      pw2.println ("Scrooge McDuck ACC 78.7 77.3 63.5");
      pw2.println ("Woody Woodbecker CS 65.6 78.3 84.7");
      pw2.println ("Scooby Doo MAT 56.8 78.2 88.4");
      pw2.println ("Spider Man CHEM 58.5 99.3 92.5");
      pw2.println ("Roger rabbit NET 66.9 39.6 86.6");
      pw2.println ("Wonder Woman CHEM 68.4 83.1 69.6");
      pw2.println ("Jane Jetson ENG 77.4 85.5 69.5");
      pw2.close ();


      options ();
    Scanner k = new Scanner (System.in);
    int choice = k.nextInt ();
k.nextLine();


    /*while (choice==1){

       ArrayList<student> student=uploadStudents("input.txt");
       //System.out.println(student);//[student@49e4cb85, student@2133c8f8, .....
       //bunch of reference?
       System.out.println("***Data uploaded successfully.\n");              
       options();

       choice=k.nextInt();

       }

       while (choice==2){

       printStudentData(in);    

       options();

       choice=k.nextInt();   
       } */

    while (choice != 6)
      {
    File in;
    switch (choice)
      {

      case 1:
        System.out.printf("What is the name of the file you want to upload data from?\n");
       
        String filename = k.nextLine ();
        switch (filename)
          {

          case "input.txt":
        in = new File (filename);
        java.util.ArrayList < student > student = uploadStudents (in);
        //System.out.println(student);//[student@49e4cb85, student@2133c8f8, .....
        //bunch of reference?
        System.out.println ("***Data uploaded successfully.\n");
        options ();
        choice = k.nextInt ();
                k.nextLine ();
        while(choice==2){
       
         printStudentData(in);    // if you don't ask again it will infinitely print;
         System.out.println("");
         
         options();
        
        choice=k.nextInt();
        k.nextLine();
        }
            
    break;

        case "input2.txt":
            in = new File (filename);
        java.util.ArrayList < student > student2 = uploadStudents (in);
        System.out.println ("***Data uploaded successfully.\n");
        options ();
        choice = k.nextInt ();
        k.nextLine();
        while(choice==2){
       
         printStudentData(in);    // if you don't ask again it will infinitely print;
         System.out.println("");
         
         options();
        
        choice=k.nextInt();
        k.nextLine();
        }    
        break;
          }
        break;
        
        case 2:
            out.printf ("no data found");   // if you don't ask again it will infinitely print;
          System.out.println ("");

          options ();

          choice = k.nextInt ();
                k.nextLine ();
          break;

        case 6:
            
            break;
default:
out.println("no data found");

      }
      }
  }

  public static void options ()
  {             //
    System.out.
      println
      ("Welcome to the Student Analyzer program. Please choose from the following options:");
    System.out.println ("1. Upload data");
    System.out.println ("2. View data");
    System.out.println ("3. Find Major");
    System.out.println ("4. Create statistics File");
    System.out.println ("5. Print Statistics File");
    System.out.println ("6. Exit the program");
  }             //


  public static ArrayList < student > uploadStudents (File inputfilename) throws IOException    ///
  {
    java.util.ArrayList < student > student = new java.util.ArrayList <> ();



    Scanner sc = new Scanner (inputfilename);

    while (sc.hasNext ())
      {
    //new student object??
    student s =
      new student (sc.next (), sc.next (), sc.next (), sc.nextFloat (),
               sc.nextFloat (), sc.nextFloat ());
      student.add (s);
      }



    return student;

  }             /// 

  public static void printStudentData (File inputfile) throws IOException
  {

    Scanner o = new Scanner (inputfile);
    while (o.hasNext ())
        System.out.
    printf ("Student: %s %s, Major: %s, Exam Grades: %.1f, %.1f, %.1f%n",
        o.next (), o.next (), o.next (), o.nextFloat (),
        o.nextFloat (), o.nextFloat ());
      o.close ();
  }



}


我能够通过使用Switch语句在情况下使其正常工作,但是我真的不可能修改代码并使其功能功能;我尝试初始化开关的文件,而当这样的循环'in = new file(“?”); 但是它只需返回thread“ main” java.io.filenotfoundexception中的异常:(否此类文件或目录)

////(old),所以基本上是在我在hackerank上运行它时,它说

Student.java:104:错误:可能没有初始化的printStudentData(in); ^ 1错误

如何重写程序,以便情况2使用案例1创建的文件?/////

//(old)Hello so I'm writing a program that ask for an option from the user and run the methods created for each one
there are 2 input files "input1.txt" and "input2.txt" that is being created but I'm not going to include them here because they take up too much space it's not relevant to the question. so I've only worked up to option 2 so far but when I tested it on hackerank something is not right.

(new)below is my code(not complete code)

update:Hello below i'll post my full revised code

import static java.lang.System.*;
import java.io.FileNotFoundException;
import java.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.ArrayList;

public class Main
{
  public static void main (String[]args) throws IOException
  {
    PrintWriter pw1 = new PrintWriter ("input.txt");
      pw1.println ("Mickey Mouse CS 98.7 67.8 23.5");
      pw1.println ("Minnie Mouse ENG 45.6 98.3 94.7");
      pw1.println ("Donald Duck NET 56.8 74.2 78.4");
      pw1.println ("Bob Builder CS 78.5 89.4 82.5");
      pw1.println ("Snow White MAT 56.6 32.4 56.6");
      pw1.println ("Hellen Keller CHEM 78.8 23.1 99.6");
      pw1.println ("Daffy Duck ENG 67.4 55.5 89.5");
      pw1.println ("Fred Flinstone MAT 45.3 87.4 38.9");
      pw1.println ("Daffy Duck CS 76.5 22.2 88.5");
      pw1.println ("Bugs Bunny NET 68.4 89.7 95.6");
      pw1.println ("Winnie Pooh CHEM 77.5 89.4 98.2");
      pw1.close ();
    PrintWriter pw2 = new PrintWriter ("input2.txt");
      pw2.println ("Scrooge McDuck ACC 78.7 77.3 63.5");
      pw2.println ("Woody Woodbecker CS 65.6 78.3 84.7");
      pw2.println ("Scooby Doo MAT 56.8 78.2 88.4");
      pw2.println ("Spider Man CHEM 58.5 99.3 92.5");
      pw2.println ("Roger rabbit NET 66.9 39.6 86.6");
      pw2.println ("Wonder Woman CHEM 68.4 83.1 69.6");
      pw2.println ("Jane Jetson ENG 77.4 85.5 69.5");
      pw2.close ();


      options ();
    Scanner k = new Scanner (System.in);
    int choice = k.nextInt ();
k.nextLine();


    /*while (choice==1){

       ArrayList<student> student=uploadStudents("input.txt");
       //System.out.println(student);//[student@49e4cb85, student@2133c8f8, .....
       //bunch of reference?
       System.out.println("***Data uploaded successfully.\n");              
       options();

       choice=k.nextInt();

       }

       while (choice==2){

       printStudentData(in);    

       options();

       choice=k.nextInt();   
       } */

    while (choice != 6)
      {
    File in;
    switch (choice)
      {

      case 1:
        System.out.printf("What is the name of the file you want to upload data from?\n");
       
        String filename = k.nextLine ();
        switch (filename)
          {

          case "input.txt":
        in = new File (filename);
        java.util.ArrayList < student > student = uploadStudents (in);
        //System.out.println(student);//[student@49e4cb85, student@2133c8f8, .....
        //bunch of reference?
        System.out.println ("***Data uploaded successfully.\n");
        options ();
        choice = k.nextInt ();
                k.nextLine ();
        while(choice==2){
       
         printStudentData(in);    // if you don't ask again it will infinitely print;
         System.out.println("");
         
         options();
        
        choice=k.nextInt();
        k.nextLine();
        }
            
    break;

        case "input2.txt":
            in = new File (filename);
        java.util.ArrayList < student > student2 = uploadStudents (in);
        System.out.println ("***Data uploaded successfully.\n");
        options ();
        choice = k.nextInt ();
        k.nextLine();
        while(choice==2){
       
         printStudentData(in);    // if you don't ask again it will infinitely print;
         System.out.println("");
         
         options();
        
        choice=k.nextInt();
        k.nextLine();
        }    
        break;
          }
        break;
        
        case 2:
            out.printf ("no data found");   // if you don't ask again it will infinitely print;
          System.out.println ("");

          options ();

          choice = k.nextInt ();
                k.nextLine ();
          break;

        case 6:
            
            break;
default:
out.println("no data found");

      }
      }
  }

  public static void options ()
  {             //
    System.out.
      println
      ("Welcome to the Student Analyzer program. Please choose from the following options:");
    System.out.println ("1. Upload data");
    System.out.println ("2. View data");
    System.out.println ("3. Find Major");
    System.out.println ("4. Create statistics File");
    System.out.println ("5. Print Statistics File");
    System.out.println ("6. Exit the program");
  }             //


  public static ArrayList < student > uploadStudents (File inputfilename) throws IOException    ///
  {
    java.util.ArrayList < student > student = new java.util.ArrayList <> ();



    Scanner sc = new Scanner (inputfilename);

    while (sc.hasNext ())
      {
    //new student object??
    student s =
      new student (sc.next (), sc.next (), sc.next (), sc.nextFloat (),
               sc.nextFloat (), sc.nextFloat ());
      student.add (s);
      }



    return student;

  }             /// 

  public static void printStudentData (File inputfile) throws IOException
  {

    Scanner o = new Scanner (inputfile);
    while (o.hasNext ())
        System.out.
    printf ("Student: %s %s, Major: %s, Exam Grades: %.1f, %.1f, %.1f%n",
        o.next (), o.next (), o.next (), o.nextFloat (),
        o.nextFloat (), o.nextFloat ());
      o.close ();
  }



}


I was able to make it work by using switch statement in side the case however it's really unfortunate that I wasn't able to modify the code and make it functional; I tried initialize the file outsiede of the switch and while loop like this 'File in=new File("?");
but it simply returns Exception in thread "main" java.io.FileNotFoundException: (No such file or directory)

////(old)so basically when I run it on hackerank it says

student.java:104: error: variable in might not have been initialized printStudentData(in); ^ 1 error

how do rewrite the program so that the case 2 will use the file created in case 1 ?////

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

触ぅ动初心 2025-01-31 21:13:12

在开始之前,您最好在中使用的默认值。之后,如果在情况1中初始化,则可以在情况2中使用它。

You would better use a default value for in before starting while . after that if you initialize in the case 1 you can use it in the case 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文