hanzhu3366 发表于 2008-3-27 10:07

很简单的一个程序,让我昏迷

下面的这个程序是给定[color=Blue]已知值[/color],求每位数字并输出:
[code]package qqqqq;

public class wwwww {
    public static void main(String[] args) {
        int m=234;
      int i_1,i_2,i_3;
      i_3=m/100;
      i_2=(m%100)/10;
      i_1=(m%100)%10;
       System.out.println(" 百位是"+i_3);
       System.out.println(" 十位是"+i_2);
       System.out.println(" 个位是"+i_1);
    }
}[/code]

如果我想输入的是[color=Magenta]任意数[/color],再输出,我该怎么做?(我们还没有学输出语句)

sunkaidong 发表于 2008-3-27 10:10

System.in.read();

新手上路了 发表于 2008-3-27 11:32

System.in()

两岁半 发表于 2008-3-28 09:53

package com;

public class EachCount {

        public static void main(String[] args) {

                CountValue a = new CountValue();
                a.count = 789;
                a.f();
        }
}

class CountValue {

        int count;

        int count_1, count_2, count_3;

        void f() {

                count_1 = count / 100;
                count_2 = (count / 10) % 10;
                count_3 = (count % 10) % 10;

                System.out.println("百位是 " + count_1);
                System.out.println("十位是 " + count_2);
                System.out.println("个位是 " + count_3);
        }

}
百位是 7
十位是 8
个位是 9

[[it] 本帖最后由 两岁半 于 2008-3-28 10:36 编辑 [/it]]

hanzhu3366 发表于 2008-3-28 15:42

回复 4# 的帖子

谢谢!

he20041987 发表于 2008-3-28 17:46

你的程序没有问题啊?应该是你自己的文件名没有和类名一样吧?这是一个公有的类 i_1=(m%100)%10; 这里没有必要这样  i_1=m%10;这样就可以了。

aipb2007 发表于 2008-3-28 20:16

你这样做,要是不是3位数呢?

两岁半 发表于 2008-3-30 10:38

如果输入不是3位,
如10,哪么百位是0,
1201,哪么百位是2,1就是百位以上的数了,

package com;
import java.util.Scanner;
public class EachCount {
        public static void main(String[] args) {
                CountValue a = new CountValue();
                System.out.println("请输入一个数:");
                a.count = a.valueIntKey();
                a.f();
        }
}
class CountValue {
        int count;
        int count_1, count1, count10, count100;
        void f() {
                count_1 = count / 100;
                count100 = count_1 % 10;
                count_1 /= 10;
                count10 = (count / 10) % 10;
                count1 = (count % 10) % 10;
                System.out.println("百位以上的数是 " + count_1);
                System.out.println("百位" + count100);
                System.out.println("十位是 " + count10);
                System.out.println("个位是 " + count1);
        }
        public int valueIntKey() {
                Scanner scanner = new Scanner(System.in);
                int inCount = scanner.nextInt();
                return inCount;
        }
}
结果:
请输入一个数:
12345
百位以上的数是 12
百位3
十位是 4
个位是 5
____________________
请输入一个数:
12
百位以上的数是 0
百位0
十位是 1
个位是 2
个位是 5

由于我自己也是初学者,考虑的不是很好,如果大家有什么更好的方法,可以交流一下.

页: [1]

编程论坛