Я начинаю, и у меня проблема с отправкой выходов в файл csci.txt. Я могу видеть все выходы в моей консоли, и он отлично работает (я думаю), но как я могу напечатать точные результаты в файле csci.txt?Как распечатать все выходы в txt-файле
Адрес Driver class
. main()
import java.io.*;
public class Driver {
public static void main (String[] args) throws IOException{
int choice;
PrintWriter pw = new PrintWriter(new FileWriter("csis.txt"));
Decimal dec = new Decimal(pw);
//Binary bin = new Binary(pw);
//Hexadecimal hex = new Hexadecimal(pw);
Menu menu = new Menu(pw);
do {
menu.display();
choice = menu.getSelection();
switch (choice) {
case 1: dec.decToBin(); break;
case 2: dec.decToHex(); break;
//case 3: bin.binToDec(); break;
//case 4: bin.binToHex(); break;
//case 5: hex.hexToDec(); break;
//case 6: hex.hexToBin(); break;
}
}while (choice != 7);
pw.close();
}
}
Здесь Menu class
:
import java.util.Scanner;
import java.io.*;
public class Menu {
Scanner kb = new Scanner(System.in);
int choice;
public PrintWriter pw;
public Menu(PrintWriter pw) {
this.pw = pw;
}
public void display(){
System.out.println("Hello, there.");
System.out.println("1: Decimal to Binary.");
System.out.println("2: Decimal to Hexadecimal.");
System.out.println("3: Binary to Decimal.");
System.out.println("4: Binary to Hexadecimal.");
System.out.println("5: Hexadecimal to Decimal.");
System.out.println("6: Hexadecimal to Binary.");
}
public int getSelection() {
System.out.println("Type in a choice number.");
int input = kb.nextInt();
choice = input;
Decimal dec = new Decimal(pw);
switch(choice){
case 1: dec.inputDecBin(); dec.decToBin(); break;
case 2: dec.inputDecHex(); dec.decToHex(); break;
}
return 0;
}
}
Здесь Decimal class
:
import java.util.Scanner;
import java.io.*;
public class Decimal{
private PrintWriter pw;
public Decimal(PrintWriter pw){
this.pw = pw;
}
Scanner kb = new Scanner(System.in);
int inDec;
int num;
int rem;//remainder
//Allows user to type in a positive decimal number
public void inputDecBin(){
System.out.println("Enter a positive decimal number. I will convert into a binary number.");
inDec = kb.nextInt();
num = inDec;
}
//converts decimal into binary
public void decToBin(){
if (num < 0){
System.out.println("Error. You should put a positive decimal number.");
}
else if (num == 0){
System.out.println("0000 0000 0000 0000 0000 0000 0000");
}
else{
int binary[] = new int[32];
for(int j=0; num>0; j++){
binary[j] = num%2;
num = num/2;
}
for(int j=31; j>=0; j--){
System.out.print(binary[j]);
if(j%4==0)
System.out.print(" ");
}
System.out.print("\n");
}
}
//Allows user to type in a positive decimal number
public void inputDecHex(){
System.out.println("Enter a positive decimal number. I will convert into a hexadecimal number.");
inDec = kb.nextInt();
num = inDec;
}
//converts decimal into hexadecimal
public void decToHex(){
String str="";
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
if(num < 0)
System.out.println("Error. You should put a positive decimal number.");
else if(num==0)
System.out.println("0");
while(num>0)
{
rem=num%16;
str=hex[rem]+str;
num=num/16;
}
System.out.println(str);
}
}