为我的2个模式输出错误的输出

时间:2014-03-27 03:39:36

标签: java

我似乎无法为模式3或4获得正确的输出。它不是替换@符号,而是将*放在它之后。这是我的Patterns.java和PatternsTest.java的代码:

模式:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package APatterns;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @author Jacob
*/
public class Patterns {

public static void pattern1 (int size)
{   
    InputStreamReader istream = new InputStreamReader (System.in);
    BufferedReader read = new BufferedReader (istream);

    System.out.print("Size: ");

    int num = 0;

    try
    {
        num=Integer.parseInt( read.readLine() );
    } 

    catch(IOException | NumberFormatException Number)
    {
        System.out.println("Invalid Number!");
    }

        for (int i = 1; i <= num; i++)
            {
                for(int j = 1; j < num - (i-1); j++)
                {
                    System.out.print(" ");
                }

                for(int k = 1; k <= i; k++)
                {
                    System.out.print("@");

                    for(int k1 = 1; k1 < k; k1 += k)
                    {
                        System.out.print("@");
                    }
                }
                System.out.println();
            }
}

public static void pattern2 (int size)
{
    InputStreamReader istream = new InputStreamReader (System.in);
    BufferedReader read = new BufferedReader (istream);

    System.out.print("Size: ");

    int num = 0;

    try
    {
        num=Integer.parseInt( read.readLine() );
    } 

    catch(IOException | NumberFormatException Number)
    {
        System.out.println("Invalid Number!");
    }

        for (int i = 1; i <= num; i++)
        {
            for (int j = i; j > 1; j--)
            {
                System.out.print(" ");
            }
            for(int k = 1; k <= num - (i-1); k++)
            {
                System.out.print("@");

                for(int k1 = 1; k1 < k; k1 += k)
                {
                    System.out.print("@");
                }
            }
                System.out.println(); 
        }
}

public static void pattern3 (int size)
{ 
    InputStreamReader istream = new InputStreamReader (System.in);
    BufferedReader read = new BufferedReader (istream);

    System.out.print("Size: ");

    int num = 0;

    try
    {
        num=Integer.parseInt( read.readLine() );
    } 

    catch(IOException | NumberFormatException Number)
    {
        System.out.println("Invalid Number!");
    }

        for (int i = 1; i <= num; i++)
            {
                for(int j = 1; j < num - (i-1); j++)
                {
                    System.out.print(" ");
                }

                for(int k = 1; k <= i; k++)
                {
                    System.out.print("@");

                    if (k == i / 2)
                    {
                        System.out.print ("*");
                    }
                    else
                    {
                        System.out.print ("@");
                    }

                    for(int k1 = 1; k1 < k; k1 += k)
                    {
                        System.out.print("@");
                    }
                }
                System.out.println();
            }
}

public static void pattern4 (int size)
{
    InputStreamReader istream = new InputStreamReader (System.in);
    BufferedReader read = new BufferedReader (istream);

    System.out.print("Size: ");

    int num = 0;

    try
    {
        num=Integer.parseInt( read.readLine() );
    } 

    catch(IOException | NumberFormatException Number)
    {
        System.out.println("Invalid Number!");
    }

        for (int i = 1; i <= num; i++)
        {
            for (int j=i; j > 1; j--)
            {
                System.out.print(" ");
            }
            for(int k = 1; k <= num - (i-1); k++)
            {
                System.out.print("@");

                if (k == i / 2)
                    {
                        System.out.print ("*");
                    }
                    else
                    {
                        System.out.print ("@");
                    }

                for(int k1 = 1; k1 < k; k1 += k)
                {
                    System.out.print("@");
                }
            }
                System.out.println(); 
        }
    }

}

PatternsTest:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package APatterns;

import java.util.Scanner;

/**
*
* @author Jacob
*/
public class PatternsTest {

public static void main(String[] args)
{
    final Scanner input = new Scanner (System.in);

    int methodNumber;

    do 
    {

        System.out.print("Choose a pattern (1-4) or 0 to quit: ");
        methodNumber = input.nextInt();

        switch (methodNumber) 
        {

        case 1:
            Patterns.pattern1(methodNumber);
            break;
        case 2:
            Patterns.pattern2(methodNumber);
            break;
        case 3:
            Patterns.pattern3(methodNumber);
            break;
        case 4:
            Patterns.pattern4(methodNumber);
            break;
        }

    } while (methodNumber != 0);
}
}

模式1和2的输出是正确的,但不是3和4。

这是输出:

Choose a pattern (1-4) or 0 to quit: 1
Size: 5
    @
   @@@
  @@@@@
 @@@@@@@
@@@@@@@@@
Choose a pattern (1-4) or 0 to quit: 2
Size: 5
@@@@@@@@@
 @@@@@@@
  @@@@@
   @@@
    @
Choose a pattern (1-4) or 0 to quit: 3
Size: 5
    @@
   @*@@@
  @*@@@@@@
 @@@*@@@@@@@
@@@*@@@@@@@@@@
Choose a pattern (1-4) or 0 to quit: 4
Size: 5
@@@@@@@@@@@@@@
 @*@@@@@@@@@
  @*@@@@@@
   @@@*@
    @@
Choose a pattern (1-4) or 0 to quit: 0

3和4应该在中间列上有一个*,如下所示:

Choose a pattern (1-4) or 0 to quit: 3
Size: 5
    *
   @*@
  @@*@@
 @@@*@@@
@@@@*@@@@
Choose a pattern (1-4) or 0 to quit: 4
Size: 5
@@@@*@@@@
 @@@*@@@
  @@*@@
   @*@
    *

1 个答案:

答案 0 :(得分:0)

我建议你学习how integer division works in Java