Get your Sentense ! By Quiw

head

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

public class NewClass2 {
public static void main(String[] atgs) throws IOException {
Scanner kb = new Scanner(System.in);
PrintStream out = new PrintStream(new File(“sentence.txt”));
int i =1;
while(true){
System.out.print(“Sentence: “);
String s = kb.nextLine().toUpperCase();
if (s.trim().equalsIgnoreCase(“stop”))break;
out.println(i + “: ” + s);
i++;

}
System.out.println(“File is saved”);
out.close();
}
}

อธิบาย source code

code นี้เป็นการให้ input ค่า string มาเก็บไว้ในไฟล์ txt โดยพิมพ์ว่า stop จะหยุดทำงานและ save file

 

 

เราพิมพ์ประโยคอะไรก็ได้ลงไปและหลังจากนั้นพิมพ์ stop ทุกคำจะถูกเก็บไว้ในไฟล์ txt

at GlurGeek.Com
เรียนวิศวกรรมศาสตร์ สาขาคอมพิวเตอร์
ชอบเล่นกีตาร์ อ่านหนังสือ และนอนหลับ
ตอนนี้กำลังหัดเขียน website

Leave a Reply

© 2022 GlurGeek.Com
Exit mobile version