控制台不打印存储在通过方法调用的变量中的整数

该程序提取数字,我希望它一直循环直到用户键入键“Q”/“q”。例如,当用户按下“O”键时,程序应该打印他们输入的数字的个位数字,对于用户输入的任何 3 位数字,依此类推。我已经创建了提取个位、十位和百位数字的方法,并将它们存储到单独类中的变量中,但是当我尝试打印这些数字时,控制台中没有打印任何内容。


import java.util.Scanner;


class Methods { 


public Methods (int value) {}


public int hundreds (int num) {

    int hund = (num /100)%10;

    return hund;

}


public int tens (int num) {

    int ten = (num / 10)%10;

    return ten;

}


public int ones (int num) {

    int one = num % 10;

    return one;

}

}



public class DigitExtractor {



public static void main(String[] args)

        throws java.io.IOException {




    char input = ' '; //initialize outside loop

    Scanner s = new Scanner(System.in);



    System.out.print("Input an integer");

    int wholeNumber = s.nextInt();

    Methods num = new Methods(wholeNumber);



    do {

        System.out.println("show (W)hole number.");

        System.out.println("show (O)nes place number.");

        System.out.println("show (T)ens place number.");

        System.out.println("show (H)undreds place number.");

        System.out.println("(Q)uit");

        System.out.println("Enter your choice: ");

        input = s.next().trim().charAt(0); //using scanner only



        if (wholeNumber == 'W' || wholeNumber == 'w') {

            System.out.println("The whole number is: " + wholeNumber);

        } else if (wholeNumber == 'O' || wholeNumber == 'o') {

            System.out.println("The ones place digit is: " + num.ones(wholeNumber));

        } else if (wholeNumber == 'T' || wholeNumber == 't') {

            System.out.println("The tens place digit is: " + num.tens(wholeNumber));

        } else if (wholeNumber == 'H' || wholeNumber == 'H') {

            System.out.println("The hundreds place digit is: " + num.hundreds(wholeNumber));

        }

    } while ((wholeNumber != 'q') && (wholeNumber != 'Q'));

}

}


米脂
浏览 107回答 1
1回答

弑天下

您的代码有一些问题需要解决才能使其正常工作:班级Method必须在班级之外DigitExtractor。您在循环中调用的方法属于该类Methods,因此您必须实例化的构造函数是Methods.您必须从变量中捕获键盘输入input。额外:你需要用 try/catch 包围你的代码。我对您的代码进行了最少的更正,并且它适用于此版本:import java.util.Scanner;public class DigitExtractor {    public static void main(String[] args) throws java.io.IOException {        try (Scanner input = new Scanner(System.in)){            System.out.print("Input an integer");            int integer = input.nextInt();            Methods num = new Methods(integer);            char keyboard;            do {                System.out.println("show (W)hole number.");                System.out.println("show (O)nes place number.");                System.out.println("show (T)ens place number.");                System.out.println("show (H)undreds place number.");                System.out.println("(Q)uit");                System.out.println("Enter your choice: ");                keyboard = input.next().charAt(0);                if (keyboard == 'W' || keyboard == 'w') {                    System.out.println("The whole number is: " + integer);                } else if (keyboard == 'O' || keyboard == 'o') {                    System.out.println("The ones place digit is: " + num.ones(integer));                } else if (keyboard == 'T' || keyboard == 't') {                    System.out.println("The tens place digit is: " + num.tens(integer));                } else if (keyboard == 'H' || keyboard == 'H') {                    System.out.println("The hundreds place digit is: " + num.hundreds(integer));                }            } while ((keyboard != 'q') && (keyboard != 'Q'));        }    }}class Methods {     public Methods (int value) {}    public int hundreds (int num) {        return (num /100)%10;    }    public int tens (int num) {        return (num / 10)%10;    }    public int ones (int num) {        return num % 10;    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java