当前位置: 代码迷 >> Java相关 >> 再帮我解决一下!!谢谢!
  详细解决方案

再帮我解决一下!!谢谢!

热度:140   发布时间:2005-09-11 09:21:00.0
再帮我解决一下!!谢谢!
  用什么函数连续接收用户输入的5个数字,并能操纵这5个数字??
是readin()么?
eg: 请用户输入5~15之间的5个数字,并用横状图表示.
         5 2 7 10 3
  *****
  **
  *******
  **********
  ***

[此贴子已经被作者于2005-9-11 21:18:51编辑过]



----------------解决方案--------------------------------------------------------
// 程序运行时,数字是一个一个输入的
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Keyboard
{
  private static String getEingabe()
  {
    String eingabe;
    BufferedReader reader;
    reader = new BufferedReader( new InputStreamReader( System.in ) );
    try
    {
      eingabe = reader.readLine();
      return eingabe;
    }
    catch ( IOException e )
    {
      e.printStackTrace();
    }
    return null;
  }

  public static int getInteger()
  {
    String eingabe;
    do
    {
      eingabe = getEingabe();
    }while(checkIntegerInput(eingabe) == false);
   
    int zahl = Integer.parseInt( eingabe );
    return zahl;
  }

  public static double getDouble()
  {
    String eingabe = getEingabe();
    double zahl = Double.parseDouble( eingabe );
    return zahl;
  }

  public static String getString()
  {
    String eingabe = getEingabe();
    return eingabe;
  }

  public static boolean checkIntegerInput(String s)
  {
    char [] c = s.toCharArray();
    for(int i = 0; i<c.length; i++)
    {
      int d = (int)c[i];
      if(d<48 || d>57)
      {
        MyException e = newMyException("Invalid input, you should input all integer number");
        return false;
      }
    }
    return true;
  }
}

class MyException
{
  MyException(String s)
  {
    System.err.println(s);
    System.out.println("Please try enter a new number between 5 and 15,inclusive 5 and 15.");
  }
  public int tryAgain()
  {
    return Keyboard.getInteger();
  }
}
public class PrintAsterisk
{
  public PrintAsterisk()
  {
    int [] nums = new int[5];
    for(int i = 0; i<nums.length; i++)
    {
      System.out.println("Please input an integer number between 5 and 15, inclusive");
      nums[i] = Keyboard.getInteger();
      while(nums[i] > 15 || nums[i] < 5)
      {
        MyException e = new MyException("invalid input");
        nums[i] = e.tryAgain();
      }
    }
    for(int i = 0; i<nums.length; i++)
    {
      for(int j = 0; j<nums[i]; j++)
      {
        System.out.print("*");
      }
      System.out.println();
    }     
  }
  public static void main(String [] args)
  {
    PrintAsterisk pa = new PrintAsterisk();
  }
}
----------------解决方案--------------------------------------------------------
import java.io.*;
class xiti3
{
public static void main (String[] arg)
{
  int a[]=0;
  DataInputStream in = new DataInputStream(System.in);
  try
  {
   for(int i=0;i<5;i++)
   {
    System.out.print("请输入1~15之间的一个数字:");
    a[i]=Integer.parseInt(in.readLine());
   }
  while (int i>0)
  {
    int i=5;
    System.out.print("您输入了以下5个数字:");
    System.out.print(a[i]);
    i--;
   }
   for(int j=0;j<5;j++)
   {
    for(int k=1;k<=a[j];k++)
    System.out.print("*");}
       System.out.println("");
      }catch(Exception e){  
  
  }
}
}
这是我写的一个,不知道哪里错了,希望大家帮我看看!!

[此贴子已经被作者于2005-9-11 21:21:09编辑过]



----------------解决方案--------------------------------------------------------
// 如果不做严格要求的话,那么勉强可以通过
import java.io.*;

class xiti3
{
  public static void main (String[] arg)
  {
    int a[]=new int[5];   // here pay attention
    DataInputStream in = new DataInputStream(System.in);
    try
    {
      for(int i=0;i<5;i++)
      {
        System.out.print("请输入1~15之间的一个数字:");
        a[i]=Integer.parseInt(in.readLine());
      }
      System.out.print("您输入了以下5个数字:");
      int i =5;                                                          // and here this while loop pay attention
      while (i>0)
      {
        System.out.print(a[5-i] + " ");
        i--;
      }
      System.out.println();
      for(int j=0;j<5;j++)
      {
        for(int k=0;k<a[j];k++)
        {
          System.out.print("*");
        }
        System.out.println();
      }
    }
    catch(Exception e)
    {  
      System.err.println(e.toString());  
    }
  }
}
----------------解决方案--------------------------------------------------------
HI! 帅哥一族 你的答案

import java.io.*;
class xiti3
{
public static void main (String[] arg)
{
  int a[]=new int[5];
  int b=5;
  DataInputStream in = new DataInputStream(System.in);
  try
  {
    for(int i=0;i<5;i++)
    {
     System.out.print("请输入1~15之间的一个数字:");
     a[i]=Integer.parseInt(in.readLine());
    }
   
    System.out.println("您输入了以下5个数字:");
   
     for (int i = 0; i<5; i++)
     {
      System.out.println (a[i]);
     }
  
      for(int j=0;j<5;j++)
      {
        
           for(int k=1;k<=a[j];k++)
           {
            System.out.print("*");
           }
         
         System.out.println("");
   }
        
    }
      catch(Exception e){ }
  
  
}
}
----------------解决方案--------------------------------------------------------
谢谢两位~~~~问题已解决!!!!!!!!!!
----------------解决方案--------------------------------------------------------
  相关解决方案