我在哪里弄错了代码?我正在尝试实现方法重载
Where I'm getting wrong with the code? I'm trying to implement the method overloading
当我 运行 输入代码时,系统提示我输入号码,我输入了号码但之后没有任何反应!我试图在这里实现方法重载。任何帮助将不胜感激。
import java.util.Scanner;
public class MethodOverload {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
Scanner inputDoub = new Scanner (System.in);
System.out.println ("Enter the int or double number");
int x = input.nextInt();
double y = inputDoub.nextDouble();
//int x;
//double y;
System.out.printf("Square of integer value %d", x, "is", square(x));
System.out.printf("Square of double value %f", y, "is", square(y));
}
public static int square(int intValue){
System.out.printf ("\nCalled square method with int argument: %d", intValue);
return intValue*intValue;
}
public static double square (double doubleValue){
System.out.printf ("\nCalled sqauer method with double argument: %d", doubleValue);
return doubleValue*doubleValue;
}
}
import java.util.Scanner;
public class MethodOverload {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.println ("Enter the int or double number");
double y = input.nextDouble();
if(y % 1 == 0) {
int x = (int) y;
System.out.printf("Square of integer value %d is %d", x, square(x));
}else{
System.out.printf("Square of double value %f is %f", y, square(y));
}
}
public static int square(int intValue){
System.out.printf ("\nCalled square method with int argument: %d", intValue);
return intValue*intValue;
}
public static double square (double doubleValue){
System.out.printf ("\nCalled sqauer method with double argument: %f", doubleValue);
return doubleValue*doubleValue;
}
}
如果我理解正确的话,您只是想获取用户输入,如果用户输入双重输入,则使用一种重载方法,如果他输入整数,则使用另一种。上面的代码就是这样做的。
它只是将用户输入存储为双精度值,如果用户输入对 1 = 0 取模(意味着它是一个整数),则将其转换为整数并调用传递整数参数的重载方法。另外,在上一个重载的 square 方法中,您在 printf 函数中使用了 %d 而不是 %f,当您想要一个 double 时使用 %f。
你的前两个printf语句也是错误的,语法只允许显示一个String,其他参数用于替换所有的%符号。
您尝试使用 %d 进行双重格式化,这是不正确的。需要更改 PFB:
public static double square (double doubleValue){
System.out.printf ("\nCalled sqauer method with double argument: %f", doubleValue);
return doubleValue*doubleValue;
}
一个观察:使用 2 个单独的 Scanner 实例没有意义。没用。
像这样更正您的代码:
Scanner input = new Scanner(System.in);
//Scanner inputDoub = new Scanner (System.in);
System.out.println ("Enter the int or double number");
int x = input.nextInt();
double y = input.nextDouble();
当我 运行 输入代码时,系统提示我输入号码,我输入了号码但之后没有任何反应!我试图在这里实现方法重载。任何帮助将不胜感激。
import java.util.Scanner;
public class MethodOverload {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
Scanner inputDoub = new Scanner (System.in);
System.out.println ("Enter the int or double number");
int x = input.nextInt();
double y = inputDoub.nextDouble();
//int x;
//double y;
System.out.printf("Square of integer value %d", x, "is", square(x));
System.out.printf("Square of double value %f", y, "is", square(y));
}
public static int square(int intValue){
System.out.printf ("\nCalled square method with int argument: %d", intValue);
return intValue*intValue;
}
public static double square (double doubleValue){
System.out.printf ("\nCalled sqauer method with double argument: %d", doubleValue);
return doubleValue*doubleValue;
}
}
import java.util.Scanner;
public class MethodOverload {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.println ("Enter the int or double number");
double y = input.nextDouble();
if(y % 1 == 0) {
int x = (int) y;
System.out.printf("Square of integer value %d is %d", x, square(x));
}else{
System.out.printf("Square of double value %f is %f", y, square(y));
}
}
public static int square(int intValue){
System.out.printf ("\nCalled square method with int argument: %d", intValue);
return intValue*intValue;
}
public static double square (double doubleValue){
System.out.printf ("\nCalled sqauer method with double argument: %f", doubleValue);
return doubleValue*doubleValue;
}
}
如果我理解正确的话,您只是想获取用户输入,如果用户输入双重输入,则使用一种重载方法,如果他输入整数,则使用另一种。上面的代码就是这样做的。
它只是将用户输入存储为双精度值,如果用户输入对 1 = 0 取模(意味着它是一个整数),则将其转换为整数并调用传递整数参数的重载方法。另外,在上一个重载的 square 方法中,您在 printf 函数中使用了 %d 而不是 %f,当您想要一个 double 时使用 %f。
你的前两个printf语句也是错误的,语法只允许显示一个String,其他参数用于替换所有的%符号。
您尝试使用 %d 进行双重格式化,这是不正确的。需要更改 PFB:
public static double square (double doubleValue){
System.out.printf ("\nCalled sqauer method with double argument: %f", doubleValue);
return doubleValue*doubleValue;
}
一个观察:使用 2 个单独的 Scanner 实例没有意义。没用。 像这样更正您的代码:
Scanner input = new Scanner(System.in);
//Scanner inputDoub = new Scanner (System.in);
System.out.println ("Enter the int or double number");
int x = input.nextInt();
double y = input.nextDouble();