2016-11-02 9 views
-4

の倍数表を印刷するプログラムを作成する必要があります。1)複数の方法があります。 2)2つの数値を読み取ります.1つは上限値、2つ目はテーブルの深さ(行と列)です。これは、たとえば10x10または12x12のような正方形のテーブルの制限から一歩上がっています。
3)ユーザーが別の入力で再度実行できるようにループがあります。 4)スキャナを使用してI/Pを読み取ります。 出力は、2つ目の番号の最初の番号の入力3と8と、このように見えるようになっている:OutputJavaで乗算表を印刷する方法

import java.util.Scanner; 
public class TimesTableRewrite 
{ 
    public static void main (String[] args) 
    { 
     Scanner in; 
     in = new Scanner (System.in); 
     boolean runAgain = false; 
     header(); 
     String response; 
     do 
     { 

      printTable(); 
      System.out.println("\nDo you want to go again? Y or N?"); 
      response = in.next(); 
      if ((response.charAt(0)=='Y'||response.charAt(0)=='y')) 
       { 
       runAgain = true; 
       } 
      else 
       { 
       runAgain = false;  
       } 
     } 
     while (runAgain); 
     footer();  
    } 

    public static void printTable() 
    { 
     Scanner in; 
     in = new Scanner (System.in); 

     int height; 
     int length; 
     System.out.println("Enter the first number to set up how far down you want the table to go."); 
     height= Integer.parseInt(in.next()); 
     System.out.println("Enter the second number to extend the table horizontally"); 
     length = Integer.parseInt(in.next()); 

     for (int i = 1; i <= height; i++) 
     { 
      for (int j = 1; j <= length; j++) //j is number of columns 
      { 
       if (i<height) 
        System.out.format("%4d", + i*j); 
       else 
        System.out.format("%4d", + i*j); 
       if (i==height) 
       { 
        System.out.println(); 
       } 
      } 
     } 
     for (int i = 1; i <= height; i++) 
     { 
      for (int j = 1; j <= length; j++) 
      { 
       if (j<length) 
        System.out.format("%4d", + i*j); 
       else 
        System.out.format("%4d", + i*j); 
       if (j==length) 
       { 
        System.out.println(); 
       } 
      } 
     } 
    } 
    public static void header() 
    { 
     System.out.println("This program will help you practice your times tables!"); 
     System.out.print("This newer version will allow you to go beyond the 12 times tables!"); 
     System.out.println("It will let you choose your upper limit \nand how deep the times table will go."); 
     System.out.println("Let's go!"); 
     System.out.println("\nPlease enter two numbers to generate a multiplication table."); 
    } 
    public static void footer() 
    { 
     System.out.println("That's all folks! See you next week!"); 
    }   
} 
+1

私は[宿題問題の学生への手紙を開く]を強くお勧めします(http://meta.programmers.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems )。 –

+0

私はあなたに私の宿題をするように求めることはありません。あなたが見ることができるように、私はすでに大部分を行っています。なぜそれが現れているのかを理解するだけで助けが必要です。私はそれに取り組んで何時間も費やした、私はそれを働かせることができないので、私はいくつかのガイダンスを求めるなら、神は禁止する。 –

答えて

2

これを置き換えます

enter image description here

しかし、それは次のように出てきます:これにより

for (int i = 1; i <= height; i++) 
{ 
    for (int j = 1; j <= length; j++) //j is number of columns 
    { 
     if (i<height) 
      System.out.format("%4d", + i*j); 
     else 
      System.out.format("%4d", + i*j); 
     if (i==height) 
     { 
      System.out.println(); 
     } 
    } 
} 
for (int i = 1; i <= height; i++) 
{ 
    for (int j = 1; j <= length; j++) 
    { 
     if (j<length) 
      System.out.format("%4d", + i*j); 
     else 
      System.out.format("%4d", + i*j); 
     if (j==length) 
     { 
      System.out.println(); 
     } 
    } 
} 

for (int i = 1; i <= height; i++) 
{ 
    for (int j = 1; j <= length; j++) //j is number of columns 
    { 
     System.out.print(i * j); 
     System.out.print("\t"); //We add a tab after each number to form every column. 
    } 

    System.out.println(); //We add a berakline for each row. 
} 
+1

"runAgain"ロジックで三項演算子を使用することができます。このように: runAgain =(response.charAt(0)== 'Y' || response.charAt(0)== 'y'); –

関連する問題