【问题标题】:how to save inputs to file and call the inputs later (after closing app)如何将输入保存到文件并稍后调用输入(关闭应用程序后)
【发布时间】:2014-09-30 04:36:38
【问题描述】:

各位,我是 Java 新手,所以请帮帮我 我正在制作一个简单的考试应用程序,老师可以输入问题 并保存 当学生打开应用程序时,系统会询问他们是否是学生,然后 应用程序将启动问题 我做了2个文件教师文件和学生文件 这是代码

package com.belal.teacher;

import java.io.*;
import java.util.Scanner;

public class Teacher {


    public static void main(String[] args) {

    // 1 is the Q
    // 2 choice 1
    // 3 choice 2
    // 4 choice 3
    // 5 correct choice 
    // 6 grade of the Q




    String userinput1;
    String userinput2;
    String userinput3;
    String userinput4;
    String userinput5;
    String userinput6;



    Scanner input1 =  new Scanner(System.in);
    Scanner input2 =  new Scanner(System.in);
    Scanner input3 =  new Scanner(System.in);
    Scanner input4 =  new Scanner(System.in);
    Scanner input5 =  new Scanner(System.in);           
    Scanner input6 =  new Scanner(System.in);

    System.out.println(" enter the question: ");
        userinput1 = input1.nextLine();
    System.out.println(" enter  answer 1 : ");
        userinput2 = input2.nextLine();
    System.out.println(" enter  answer 2 : " );
        userinput3 = input3.nextLine();
    System.out.println(" enter  answer 3 : " );
        userinput4 = input4.nextLine();
    System.out.println(" enter the correct answer : " );
        userinput5 = input5.nextLine();
    System.out.println("enter the grade of this Q : ");
        userinput6 = input6.nextLine();


        }
}

我想在 else 下调用这个类的输入法 我想在 Else 下调用那里的输入

package com.belal.student;

import java.util.Scanner;
import com.belal.teacher.Teacher;

public class Student {


    public static void main(String[] args) {

        System.out.println(" type (a) if u are a student type (b) if u are a teacher");
        Scanner input =  new Scanner(System.in);
        input.nextLine();



        if (input != input ) {
            Teacher.main(args);

        }else {
            System.out.println();



        }
    }

}

【问题讨论】:

  • 您需要将问题存储在数据库中
  • 您需要将数据存储在数据库或文本文件中。

标签: java methods input save call


【解决方案1】:

要准确理解您要做什么有点困难。至少,您似乎正在尝试读取/写入数据库。如果这只是为了学习目的并且读/写部分不是主要重点,您可以只读/写文本文件。 (尽管如果这应该是多线程的,那是一个伪劣的解决方案)。这是我不久前写的一些代码,用于快速“n”脏读/写文本。

import java.io.*;
import java.util.ArrayList;

public class TextIO {

/** Write a string to a text file at the given directory.
 * @param f - directory, a string
 * @param s - text to write
 * @throws IOException
 */
public static void write(String f, String s) throws IOException {
    write(new File(f), s);
}

/** Write a string to a text file at the given directory.
 * @param f - directory, a file
 * @param s - text to write
 * @throws IOException
 */
public static void write(File f, String s) throws IOException {
    FileWriter fr= new FileWriter(f);
    BufferedWriter br= new BufferedWriter(fr);

    br.write(s);

    br.flush();
    br.close();
}

/** Reads File f as a text file.
 * @param f - the File to read
 * @return - a String[], each entry of which is a line of text in f
 * @throws IOException - if the file reading goes bad.
 */
public static String[] read(File f) throws IOException {
    FileReader fr = null;
    BufferedReader br = null;
    try {
        fr = new FileReader(f);
        br = new BufferedReader(fr);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    ArrayList<String> s = new ArrayList<String>();

    while(br.ready()){
        s.add(br.readLine());
    }

    br.close();
    String[] sArray = new String[s.size()];
    sArray = s.toArray(sArray);
    return sArray;
}

}

如果您需要除此之外的更具体的帮助,请尝试将您的问题分解为您要完成的离散任务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多