首先,我必須道歉,因爲有這麼多類似的問題,但我不太明白。我正在學習Java教程,但我被困在這裏。如何將用戶的輸入保存到單獨的文本文件
如何將用戶的輸入存儲到文本文件中?
這是我的代碼。
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class UserInput {
public static void main(String[] args) {
ArrayList<String> fullName = new ArrayList<String>();
ArrayList<String> userId = new ArrayList<String>();
ArrayList<String> qA = new ArrayList<String>();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Please enter your Full Name: ");
fullName.add(sc.nextLine());
System.out.println("Please enter your ID: ");
userId.add(sc.nextLine());
System.out.println("What is your race/ethinicity? A. Hispanic/Latino, B. Asian, C. Caucasian, D. African American: ");
qA.add(sc.nextLine());
System.out.println("What is your preferred language? A. Spanish, B. English, C. Portugues, D. French: ");
qA.add(sc.nextLine());
System.out.println("What is your political affiliation? A. Republican, B. Democrat, C. Green, D. None: ");
qA.add(sc.nextLine());
System.out.println("What is your religious practice? A. Christian, B. Islam, C. Judaism, D. Hinduism: ");
qA.add(sc.nextLine());
System.out.println("What is your sexual orientation? A. Homosexual, B. Heterosexual, C. Bisexual, D. Asexual: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite ice cream? A. Vanilla, B. Chocolate, C. Banana, D. Gum: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite activity? A. Running, B. Walking, C. Sleeping, D. Eating: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite color? A. Burgundy, B. Black, C. Gold, D. Blue: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite clothing item? A. Shoes, B. Hats, C. Shirts, D. Rings: ");
qA.add(sc.nextLine());
System.out.println("What is your favorite pet? A. Dog, B. Cat, C. Tiger, D. Turtle: ");
qA.add(sc.nextLine());
Random random = new Random();
for (int i =0; i < 1; i++) {
int answerScore = random.nextInt(10) + 1;
System.out.println("Test Score " + answerScore + "/10");
}
break;
}
}
}
我想知道如何將用戶的姓名,ID,和答案存儲到一個單獨的.txt文件。
在此先感謝!
嗨,歡迎來到StackOverflow。這不是一個代碼寫入服務,也不是一個教程服務。從你發佈的內容來看,你實際上沒有嘗試過任何東西。 Java教程涵蓋了這個主題 - 如果您遇到麻煩,請發佈您嘗試過的以及發生了什麼。請參閱[問]在這裏提出問題的指導。 – betseyb