英文:
Running of code for triangle after user input
问题
import java.util.Scanner;
enum Type {EQUILATERAL, ISOSCELES, SCALENE, NOTTRIANGLE}
// S T A R T T R I A N G L E C L A S S
class PossibleTriangle
{
// ... (rest of the class remains the same)
// Display method
public void displayInfo ()
{
PossibleTriangle t = new PossibleTriangle (a, b, c);
System.out.printf ("\tCharacter is %n. Its type is ");
getType(pt);
}
}
// E N D T R I A N G L E C L A S S
// S T A R T M A I N C L A S S
class Test
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
int a;
int b;
int c;
Type pt;
// READ FIRST TRIANGLE
System.out.printf ("Enter 3 numbers: ");
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
PossibleTriangle p = new PossibleTriangle (a, b, c);
p.displayInfo();
// READ SECOND TRIANGLE
System.out.printf ("%nEnter 3 numbers: ");
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
}
}
Note: I've made the necessary corrections to the code to address the issues you were facing. I've removed the problematic characters like <
, >
, &
, and replaced them with their respective symbols. I've also fixed the input reading and method calls.
英文:
im doing an assignment and would require to user to input 3 number int 1-9 and then determine if it is a triangle
- (a=4, b=5, c=6 is a triangle)
- (a=1, b=2, c=2 is not a triangle)
and from there determine if it is an
- Equilateral (3 sides are equal)
- Isosceles (2 side are equal)
- Scalene (3 sides are different)
- NotTriangle
the code i've done so far has some troubles running them which i cant seem to identify where is the problem. it keeps coming out with errors from the sub class method named displayInfo
which links via method isTriangle2
& getType
to determine both if it is a triangle and which type if it is.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method printf(String, Object...) in the type PrintStream is not applicable for the arguments (String, void)
at PossibleTriangle.displayInfo(Test.java:187)
at Test.main(Test.java:225)
here is my full code for reference:
import java.util.Scanner;
enum Type {EQUILATERAL, ISOSCELES, SCALENE, NOTTRIANGLE}
// S T A R T T R I A N G L E C L A S S
class PossibleTriangle
{
// Declare instance variables related to Health Profile
private int a;
private int b;
private int c;
private Type pt;
// Default Constructor
public PossibleTriangle ()
{
//by default
}
// Other Constructor
public PossibleTriangle (int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
// Copy Constructor
public PossibleTriangle (PossibleTriangle p)
{
this(p.a, p.b, p.c);
}
// Accessor Methods
public int getA ()
{
return a;
}
public int getB ()
{
return b;
}
public int getc ()
{
return c;
}
public Type getPt ()
{
return pt;
}
// Mutator Methods
public void setInfo (int a, int b, int c, Type pt)
{
this.a = a;
this.b = b;
this.c = c;
this.pt = pt;
}
private boolean isTriangle (int a, int b, int c)
{
if ((a + b <= c) || (a + c <= b) || (b + c <= a))
return true;
else
return false;
}
private void isTriangle2 (int a, int b, int c)
{
if ((isTriangle(a,b,c)) == true)
System.out.printf("Valid Triangle%n");
else
System.out.printf("Not a Triangle%n");
}
private static Type getType2 (int a, int b, int c, Type pt)
{
// Divide character set into four types
if ((a == b) && (b == c) && (c == a)) {
return Type.EQUILATERAL;
} else if ((a == b && b != c) || (a != b && c == a) || (c==b && c != a)) {
return Type.ISOSCELES;
} else if ((a != b) && (b != c) && (c != a)) {
return Type.SCALENE;
} else {
return Type.NOTTRIANGLE;
}
}
private void getType (Type pt)
{
switch (pt){
case EQUILATERAL: System.out.printf("Equilateral Triangle");
break;
case ISOSCELES: System.out.printf("Isosceles Triangle");
break;
case SCALENE: System.out.printf("Scalene Triangle");
break;
default: System.out.printf("Not a triangle");
break;
}
}
// Display method
public void displayInfo ()
{
PossibleTriangle t = new PossibleTriangle (a, b, c);
System.out.printf ("\tCharacter is %n. Its type is " , this.getType(pt));
}
}
// E N D T R I A N G L E C L A S S
// S T A R T M A I N C L A S S
class Test
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
int a;
int b;
int c;
Type pt;
// READ FIRST TRIANGLE
System.out.printf ("Enter 3 numbers: ");
a = input.next ().charAt (0);
b = input.next ().charAt (0);
c = input.next ().charAt (0);
PossibleTriangle p = new PossibleTriangle (a, b, c);
p.displayInfo();
// READ SECOND TRIANGLE
System.out.printf ("%nEnter 3 numbers: ");
a = input.next ().charAt (0);
b = input.next ().charAt (0);
c = input.next ().charAt (0);
}
}
答案1
得分: 0
很难通过阅读你的描述找出你的任务是什么,但似乎你需要编写两个方法:
-
一个名为
getType(int a, int b, int c)
的方法,根据参数a
、b
和c
返回三角形的类型。这个方法将使用一个if
-else
语句。 -
一个用于显示类型的方法,接受一个
Type
对象作为参数。这个方法将使用一个switch
语句。
代码大致如下:
private static Type getType(int a, int b, int c) {
if ( /* 条件 1 */ ) {
return NOTTRIANGLE;
} else if ...
...
...
}
private static void displayAnalysis() {
int a = /* ... */ ;
int b = /* ... */ ;
int c = /* ... */ ;
Type type = getType(a, b, c); // <-- 此处调用 getType 方法以确定类型
switch (type) {
case NOTTRIANGLE:
...
break;
case EQUILATERAL:
...
break;
...
...
}
}
请注意,在 if
-else
语句中,条件按顺序检查,并且一旦满足一个条件,就不会继续检查剩下的条件。因此,你需要考虑以何种顺序检查不同类型的三角形。
英文:
It is difficult to find out what your assignment is by reading your description, but it seems that you need to write two methods:
-
A method
Type getType(int a, int b, int c)
which returns the type of the triangle given the parametersa
,b
andc
. This method would use anif
-else
statement. -
A method which displays the type given a
Type
object. This method would use aswitch
statement.
It will look something like this:
private static Type getType(int a, int b, int c) {
if ( /* condition 1 */ ) {
return NOTTRIANGLE;
} else if ...
...
...
}
private static void displayAnalysis() {
int a = /* ... */ ;
int b = /* ... */ ;
int c = /* ... */ ;
Type type = getType(a, b, c); // <-- here getType is called to determine the type
switch (type) {
case NOTTRIANGLE:
...
break;
case EQUILATERAL;
...
break;
...
...
}
}
Note that the conditions in an if
-else
statement and checked in order, and that the rest of the conditions is not checked as soon as one conditions is satisfied. So, you need to think about in what order to check for the different types of triangle.
答案2
得分: 0
我认为这个解决方案不是最佳的。首先,我们必须理解如何使用 switch 语句(阅读 Oracle switch 页面)。看看这个图片:
每个 case 语句都是独立的,因此你不能在 switch case 中使用 if...else
。正确的形式是:
switch (Type){
case CASE1: //op1;
break;
case CASE2: //op2;
break;
[ . . . ]
case CASE N: //opN;
break;
}//switch
注意,在每个代码块的末尾都要写上 break;。
当你使用 switch 语句时,已经在做类似于 if-else
的工作,所以不应该使用 switch 进行其他类型的检查。你可能需要根据条件将当前的 Type
保存在一个变量中,然后使用 switch 指示要执行哪些操作。
例如:
enum Type {EQUILATERAL, ISOSCELES, SCALENE, NOTTRIANGLE}
/*
int a = ...
int b = ...
int c = ...
*/
Type currentType = EQUILATERAL;
/*现在检查三角形的类型...我不想改变你的代码,但是条件中有一些错误(第二个 "else" 必须写在第一个 else 之前...)*/
if ((a + b <= c) || (a + c <= b) || (b + c <= a)) {
currentType = NOTTRIANGLE;
} else if ((a == b) || (b == c) || (c == a)) {
currentType = ISOSCELES;
} else if ((a == b) && (b == c) && (c == a)) {
currentType = EQUILATERAL;
} else {
currentType = SCALENE;
}
//现在你可以使用 switch
switch (currentType){
case NOTTRIANGLE: System.out.printf("Not a triangle");
//其他操作
break;
case ISOSCELES: System.out.printf("Isosceles Triangle");
//其他操作
break;
case EQUILATERAL: System.out.printf("Equilateral Triangle");
//其他操作
break;
default: System.out.printf("Scalene Triangle");
//其他操作
break;
}
显然,你可以将 if-else 语句和 switch 放在不同的方法中,并从主方法中调用它。
如果你想要获取用户输入的三个数字,可以使用 java.util.Scanner; 和它的 nextInt() 方法,允许读取用户输入的数字。
这是代码示例:
Scanner sc = new Scanner(System.in);
System.out.print("Enter A: ");
int a = sc.nextInt();
System.out.print("Enter B: ");
int b = sc.nextInt();
System.out.print("Enter C: ");
int c = sc.nextInt();
英文:
I don't think that this solution is the best.
First of all, we have to understand how the switch works (read the Oracle switch page). Look at this image:
Every case statement is indipendent, so you can't use if...else
accross the switch case.
The correct form is:
switch (Type){
case CASE1: //op1;
break;
case CASE2: //op2;
break;
[ . . . ]
case CASE N: //opN;
break;
}//switch
Note that we have to write break; at the end of every block.
When you use a switch you are already doing a job similar to an if-else
, so you should not use the switch to do other checks of this type.
You, probably, have to save the current Type
in a variable (depending from the conditions) and use the switch to indicate what operations to perform.
For example:
enum Type {EQUILATERAL, ISOSCELES, SCALENE, NOTTRIANGLE}
/*
int a = ...
int b = ...
int c = ...
*/
Type currentType = EQUILATERAL;
/*Now check the type of the triangle... I don't want to change your code,
but there are some errors in the conditions (the second "else" must be
written before the first else...)
*/
if ((a + b <= c) || (a + c <= b) || (b + c <= a)) {
currentType=NOTTRIANGLE;
} else if ((a == b) || (b == c) || (c == a)) {
currentType=ISOSCELES;
} else if ((a == b) && (b == c) && (c == a)) {
currentType=EQUILATERAL;
} else {
currentType=SCALENE
}
//Now you can use the switch
switch (currentType){
case NOTTRIANGLE: System.out.printf("Not a triangle");
//other operations
break;
case ISOSCELES: System.out.printf("Isosceles Triangle");
//other operations
break;
case EQUILATERAL: System.out.printf("Equilateral Triangle");
//other operations
break;
default: System.out.printf("Scalene Triangle");
//other operations
break;
}
You can, obviously, put the if-else statement and the switch in different methods and call it from the main method.
If you want to get the 3 numbers as a user input, you can use java.util.Scanner; and its method nextInt(), that allows to read a number digited by the user.
Here's the code:
Scanner sc = new Scanner(System.in);
System.out.print("Enter A: ");
int a = sc.nextInt();
System.out.print("Enter B: ");
int b = sc.nextInt();
System.out.print("Enter C: ");
int c = sc.nextInt();
专注分享java语言的经验与见解,让所有开发者获益!
评论