2017-10-16 5 views
0

私は非常にJavaに新しいですし、私は割り当てに少し助けが必要です。割り当ては、ユーザー入力(半径、x座標、& y座標)を取得して、3つの異なる色の円をdrawingPanelに描画することでした。 2番目の部分では、2つの円の半径を比較し、一方が他方よりも小さいか大きいか、同じサイズかをユーザーに知らせる静的メソッドを要求します。私は、2つを比較する方法で半径の入力をどのように使用するかを考え出すのに問題があります。ここでdrawingpanelのユーザー入力を取得し、別の方法で使用します

は、これまでの私のコードです:

import java.awt.*; 
import java.util.*; 
public class Circles { 
    public static final Scanner CONSOLE = new Scanner(System.in); 


    public static void blueCircle(Graphics g) { 
    g.setColor(Color.BLUE); 
    int r = CONSOLE.nextInt(); 
    int x = CONSOLE.nextInt(); 
    int y = CONSOLE.nextInt(); 
    g.fillOval(0 + x, 0 + y, r * 2, r * 2); 
    } 
    public static void greenCircle(Graphics g) { 
    g.setColor(Color.GREEN); 
    int r = CONSOLE.nextInt(); 
    int x = CONSOLE.nextInt(); 
    int y = CONSOLE.nextInt(); 
    g.fillOval(0 + x, 0 + y, r * 2, r * 2); 
    } 
    public static void redCircle(Graphics g) { 
    g.setColor(Color.RED); 
    int r = CONSOLE.nextInt(); 
    int x = CONSOLE.nextInt(); 
    int y = CONSOLE.nextInt(); 
    g.fillOval(0 + x, 0 + y, r * 2, r * 2); 

    } 
    public static void compareCircles(int r1, int r2) { 
    int x; 
    if (r1 < r2) 
     x = -1; 
    if (r1 == r2) 
     x = 0; 
    if (r1 > r2) 
     x = 1; 
    return; 
    } 
    public static void main(String[] args) { 
    DrawingPanel panel = new DrawingPanel(400, 300); 
    Graphics g = panel.getGraphics(); 
    System.out.println("Enter values for the radius, x , & y-coordinates of blue circle: "); 
    blueCircle(g); 
    System.out.println("Enter values for the radius, x , & y-coordinates of green circle: "); 
    greenCircle(g); 
    System.out.println("Enter values for the radius, x , & y-coordinates of red circle: "); 
    redCircle(g); 

    } 


} 

答えて

0

あなたの実装では、コードの再利用を減らすことができます。与えられた入力パラメータに対して円を作成する一般的な方法を作成します。

public static void blueCircle(Graphics g, int r, int x, int y, Color c) { 
    g.setColor(c); 
    g.fillOval(0 + x, 0 + y, r * 2, r * 2); 
} 

次に、半径比較の一般的な方法の1つです。

public static String compareCircles(int r1, int r2) { 
     String output = ""; 
     if (r1 < r2) 
      output = r1+" circle is smaller than "+r2; 
     if (r1 == r2) 
      output = "both circles are in same size."; 
     if (r1 > r2) 
      output = r1+" circle is larger than "+r2; 
     return output; 
} 

ここでメインのmehod内で必要な入力を取得し、これらのメソッドに渡します。これはあなたが探しているものである

public static void main(String[] args) { 
    DrawingPanel panel = new DrawingPanel(400, 300); 
    Graphics g = panel.getGraphics(); 
    System.out.println("Enter values for the radius, x , & y-coordinates of blue circle: "); 
    int rBlue = CONSOLE.nextInt(); 
    int xBlue = CONSOLE.nextInt(); 
    int yBlue = CONSOLE.nextInt(); 
    // Better to do the validation for the user inputs 
    blueCircle(g, rBlue, xBlue, yBlue, Color.BLUE); 
    // Do the same thing for the other two circles. 
    // Then call the comparison method with your r input values like below. 
    //String output = compareCircles(rBlue, rGreen); 

}

・ホープ..

+0

私は比較法3回呼び出すことで、お互いにそれぞれの円を比較する必要があれば、私は何をすべき。例:compareCircles(rb、rg); compareCircles(rb、rr); compareCircles(rr、rg); –

+0

あなたは上記のコメントで述べたように、3回後にcomaparison関数を3回呼び出す必要があります。 – Neero

+0

これはあなたのために機能しますか?なにか手伝うことはありますか – Neero

関連する問題