我有一个程序,最初将两个数字传递给一个方法并返回更高的数字。我得到了所以我决定扩展到该程序,并希望程序向用户询问2个数字。我有两个问题,我无法弄清楚。首先是说我的变量i和j没有被初始化。第二个是程序循环3次。有人可以给我任何帮助。我来自c#谢谢。
package javabook;
import java.util.Scanner;
public class Chapter5 {
public static void main(String[] args)
{
int i;
int j;
int k = max(num1(i),num2(j));
//Scanner input = new Scanner(System.in);
num1(i);
num2(j);
System.out.print("The maximum between " + num1(i) + " and " + num2(j) + " is " +k);
}
//Return the max between two numbers
public static int max(int num1, int num2)
{
int result;
if (num1>num2)
result = num1;
else
result = num2;
return result;
}//End Max Method
public static int num1(int i)
{
Scanner input = new Scanner(System.in);
System.out.print("Please enter the first number: ");
input.nextInt();
input.close();
return i;
}//End num1 method
public static int num2(int j)
{
Scanner input = new Scanner(System.in);
System.out.print("Please enter the second number: ");
input.nextInt();
input.close();
return j;
}//end num2 method
}
答案 0 :(得分:0)
这里:
int i;
int j; <---creates the var, but doesn't initialize it
int k = max(num1(i),num2(j));
^---using the var, without having initialized it
即使是简单的
j = 0;
会有所帮助。
答案 1 :(得分:0)
局部变量与全局声明的变量没有相同的值,因此您必须指定一些值才能使用它们。全局变量int默认值为0。
use strict;
use warnings;
my $flag =0;
my @locations= (
"/path/with/foo/HELLO",
"/path/with/foo/def-abc-addons.install",
"/path/with/foo/def-abc-addons.lintian-overrides",
"/path/with/foo/def-abc-addons.postrm",
"/abc/def/ggfg",
"/frgrg/hjytj/dgth",
);
foreach my $location(@locations){
if ($location =~ m/\/foo\/HELLO/) {
print 'match';
$flag=1;
} else {
print 'no match';
$flag=0
}
}