Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用printwriter对象连接两个类的Java_Java_Object_Methods_Constructor_Printwriter - Fatal编程技术网

用printwriter对象连接两个类的Java

用printwriter对象连接两个类的Java,java,object,methods,constructor,printwriter,Java,Object,Methods,Constructor,Printwriter,我在这里有很多麻烦。我需要将代码与以下驱动程序类以及十进制类、菜单类、二进制类和十六进制类匹配。正如您在下面看到的驱动程序类,我编写了十进制类。我知道它制作得很糟糕,但我遇到的最大问题是我不知道如何使用printwriterobject和其他方法将所有输出发送到csis.txt。Eclipse中的感叹号表示Decimal dec=new Decimal(pw)表示未使用局部变量的值。另外,如何在十进制类中使用Decimal.pw 这是我需要使用的驱动程序类 import java.io.*; i

我在这里有很多麻烦。我需要将代码与以下
驱动程序类
以及
十进制类
菜单类
二进制类
十六进制类
匹配。正如您在下面看到的
驱动程序类
,我编写了
十进制类
。我知道它制作得很糟糕,但我遇到的最大问题是我不知道如何使用
printwriter
object和其他方法将所有输出发送到csis.txt。Eclipse中的感叹号表示
Decimal dec=new Decimal(pw)表示未使用局部变量的值。另外,如何在
十进制类中使用
Decimal.pw

这是我需要使用的
驱动程序类

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

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.print("Sends to output file.");
        pw.println("Sends to output file with linefeed.");
        pw.close();
    }
}
这是一个
十进制类

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

public class Decimal{
    Scanner kb = new Scanner(System.in);
    int inDec;  
    int num;
    int rem;//remainder

    private PrintWriter pw;
    public Decimal(PrintWriter pw) {
        this.pw = pw;
    }

    //Allows user to type in a positive decimal number
    public void inputDec(){
        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 inputHex(){
        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'};

         while(num>0)
         {
           rem=num%16; 
           str=hex[rem]+str; 
           num=num/16;
         }
         System.out.println(str);
    }
}
import java.util.Scanner;
导入java.io.*;
公共类十进制{
扫描仪kb=新扫描仪(System.in);
国际指数;
int-num;
int rem;//余数
私人印刷作家;
公共十进制(PrintWriter pw){
this.pw=pw;
}
//允许用户键入十进制正数
public void inputDec(){
System.out.println(“输入一个正十进制数,我将转换成二进制数。”);
inDec=kb.nextInt();
num=inDec;
}
//将十进制转换为二进制
公共无效decToBin(){
if(num<0){
System.out.println(“错误。您应该输入一个正的十进制数字。”);
}
else if(num==0){
系统输出打印项次(“0000”);
}
否则{
int binary[]=新int[32];
对于(int j=0;num>0;j++){
二进制[j]=num%2;
num=num/2;
} 
对于(int j=31;j>=0;j--){
系统输出打印(二进制[j]);
如果(j%4==0)
系统输出打印(“”);
}
系统输出打印(“\n”);
}
}
//允许用户键入十进制正数
public void inputex(){
System.out.println(“输入一个正十进制数,我将转换成十六进制数。”);
inDec=kb.nextInt();
num=inDec;
}
//将十进制转换为十六进制
公共无效decToHex(){
字符串str=“”;
字符十六进制[]={0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(num>0)
{
rem=num%16;
str=hex[rem]+str;
num=num/16;
}
系统输出打印项次(str);
}
}

检查
PrintWriter
二进制文件中的使用情况如何?在
十进制
中它的用途是什么?谢谢您的评论。它应该在csis.txt中打印出所有类型的输出或结果,如果这是您所要求的。这里有一个提示,
System.out
是一种
PrintWriter
写入标准输出。我不明白。。。你能详细解释一下吗?