你如何将一串数字拆分成单独的整数并在循环中使用它们? (Java)的

时间:2017-11-27 19:57:13

标签: java loops relationships

我有一个项目,其中我必须创建多个类,形成每个(ex:Digit0,Digit1,...,Digit9)的图像,具有小尺寸和大尺寸。有10个不同的类,所以我只是简化什么是重要的。 (例如,类Digit1包含输出小数字1或大数字1的打印功能)。我没有问题为这些数字创建类,我被困在于找出测试程序。

测试程序应该允许用户输入一个数字(例如:1,25,4354435454等)并输入一个尺寸(1表示小,2表示大)并打印出所需的图像。到目前为止,我有这个代码,它的工作原理,但它只允许单个数字

import java.util.Scanner;
public class DigitDisplay
{
    public static void main (String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int digits = scan.nextInt();
        int segmentSize = scan.nextInt();
        while ((digits!=0)&&(segmentSize!=0)) //terminates when 0 0 is input
        {
            if (digits==0)
            {
                if (segmentSize==1) //this is the small size
                {
                    Digit0 small = new Digit0(1); 
                         //this references the small sized 0 created as a method in class Digit0
                    System.out.println(small.toString());                
                         //this prints the small digit 0
                }
                else //this is the large size
                {
                    Digit0 big = new Digit0(2);
                    System.out.println(big.toString()); 
                }
            } 
            //...the other digits are placed as else ifs
        }
    }
}  

我尝试更改扫描仪对象,以便它采用字符串数字而不是int数字。所以我可以简单地拆分它并使用for循环来遍历字符串的每个字符,但我似乎无法让它工作。我真的希望我在这里有道理。我是初学者,非常感谢帮助

import java.util.Scanner;
public class DigitDisplay
{
    public static void main (String[] args)
    {
        Scanner scan = new Scanner (System.in); 
        String digits = scan.next(); //takes in a string of numbers
        digits.split(" "); //splits the string into its digits
        //int segmentSize = scan.nextInt();  commenting this out because it works. just need to focus on the 
                                             digits themselves
        while ((!digits.equals("0")) && (segmentSize!=0)) //terminates when input is 0 0 
        {
            for (int i=0; i<digits.length(); i++) //goes through all digits of string
            {
                int num = digits.charAt(i);
                switch (num)
                {
                case 0:
                    System.out.println("zero");  //there is a longer code referencing the two sizes but the sizes work but i simplified it again. this is just for me to know whether it is printing the right thing
                    break;
                default:
                    System.out.println("other"); //these are the other digits, but i just condensed them together just to see if its printing right
                    break;
                }
             }
             digits = scan.next();
             digits.split(" ");
             //segmentSize = scan.nextInt();
       }
    }
}

当我输入002时,我想输出:

zero
zero
other

但相反,它只为所有三个输出“其他”。

1 个答案:

答案 0 :(得分:0)

看看这个问题,我认为这是你正在寻找的:

Scanner scan = new Scanner (System.in); 
String digits = scan.nextLine(); //takes in a string of numbers.

int[] digits_split = new int[0];  //creates an int array to store split digits.
digits_split = digits.split(" "); //splits the string into the digits_split array.

通过创建一个int数组,现在可以更容易地验证数字。

现在您可以使用此循环检查您的分割数字:

下面注意伪代码,尚未经过测试......

      for(int i = 1; i <= digits.length; i++)
     {
         if(digits_split[i]=0)
             {
               System.out.println("zero");
             } 
          else 
            {
                System.out.println("other");
            }
     }

同时确保在输入数字时在每个数字之间放置一个空格,以便当程序请求您输入的数字时:0 0 2

编辑:

如果您的数字包含逗号,请使用:

   digits = digits.replace(",","");

此外,一旦你拆分字符串使用trim:

   digits = digits.trim();

它整理了一些东西。

ALSO:

  

当我输入002时,我想输出:

您需要输入:(0 [space] 0 [space] 2)以获得所需的输出。当你在&#34; &#34 ;.否则使用符号。

希望这有帮助,

罗布。