Java 代码';运行';永远在爪哇
我对Java相当陌生,我发现解决这个问题很困难。基本上,代码获取一个数字,并在函数generateVector中生成一个向量。当我运行这段代码时,我被要求输入一个数字,然后软件将永远运行。如果可能的话,你们能在没有其他高级功能的情况下帮助我吗?我还在学习。谢谢Java 代码';运行';永远在爪哇,java,Java,我对Java相当陌生,我发现解决这个问题很困难。基本上,代码获取一个数字,并在函数generateVector中生成一个向量。当我运行这段代码时,我被要求输入一个数字,然后软件将永远运行。如果可能的话,你们能在没有其他高级功能的情况下帮助我吗?我还在学习。谢谢 import java.util.Scanner; public class Atividade02 { static Scanner dados = new Scanner(System.in); static int
import java.util.Scanner;
public class Atividade02 {
static Scanner dados = new Scanner(System.in);
static int n;
//Main
public static void main(String args[]){
System.out.println("Type a number: ");
n = dados.nextInt();
int[] VetorA = generateVector(n);
for(int i=0; i<VetorA.length; i++){
System.out.println("Position: "+ VetorA[i]);
}
}
//Função
public static int[] generateVector(int n){
int[] VetorA = new int [n];
for (int i=0; i<n; i++){
VetorA[i] = dados.nextInt();
}
return VetorA;
}
}
import java.util.Scanner;
公共类ADIDE02{
静态扫描仪dados=新扫描仪(System.in);
静态int n;
//主要
公共静态void main(字符串参数[]){
System.out.println(“键入一个数字:”);
n=dados.nextInt();
int[]VetorA=生成因子(n);
对于(int i=0;i
我被要求输入一个数字,然后软件将永远运行
您是否输入了generativector
所需的n
编号?该程序可能只是在用户输入时被阻止了
我被要求输入一个数字,然后软件将永远运行
您是否输入了generativector
所需的n
编号?该程序可能只是在用户输入时被阻止了
我被要求输入一个数字,然后软件将永远运行
您是否输入了generativector
所需的n
编号?该程序可能只是在用户输入时被阻止了
我被要求输入一个数字,然后软件将永远运行
您是否输入了generateVector所需的n
编号?该程序可能刚在用户输入时被阻止。尝试按如下方式修改该类:
import java.util.Scanner;
public class Atividade02 {
// Added private access modifiers for properties.
// It's not necessary here, but as a general rule, try to not allow direct access to
// class properties when possible.
// Use accessor methods instead, it's a good habit
private static Scanner dados = new Scanner(System.in);
private static int n = 0;
// Main
public static void main(String args[]){
// Ask for vector size
System.out.print("Define vector size: ");
n = dados.nextInt();
// Make some space
System.out.println();
// Changed the method signature, since n it's declared
// as a class (static) property it is visible in every method of this class
int[] vetorA = generateVector();
// Make some other space
System.out.println();
// Show results
for (int i = 0; i < vetorA.length; i++){
System.out.println("Number "+ vetorA[i] +" has Position: "+ i);
}
}
// The method is intended for internal use
// So you can keep this private too.
private static int[] generateVector(){
int[] vetorA = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Insert a number into the vector: ");
vetorA[i] = dados.nextInt();
}
return vetorA;
}
}
import java.util.Scanner;
公共类ADIDE02{
//为属性添加了专用访问修饰符。
//这里没有必要,但作为一般规则,尽量不允许直接访问
//如果可能,请初始化属性。
//改用访问器方法,这是一个好习惯
专用静态扫描仪dados=新扫描仪(System.in);
私有静态int n=0;
//主要
公共静态void main(字符串参数[]){
//询问向量大小
System.out.print(“定义向量大小:”);
n=dados.nextInt();
//腾出一些空间
System.out.println();
//更改了方法签名,因为它已声明
//作为类(静态)属性,它在该类的每个方法中都是可见的
int[]vetorA=generativector();
//腾出一些空间
System.out.println();
//显示结果
for(int i=0;i
此外,当命名变量符合Java命名约定时,只有类以大写字母开头。尝试按如下方式修改类:
import java.util.Scanner;
public class Atividade02 {
// Added private access modifiers for properties.
// It's not necessary here, but as a general rule, try to not allow direct access to
// class properties when possible.
// Use accessor methods instead, it's a good habit
private static Scanner dados = new Scanner(System.in);
private static int n = 0;
// Main
public static void main(String args[]){
// Ask for vector size
System.out.print("Define vector size: ");
n = dados.nextInt();
// Make some space
System.out.println();
// Changed the method signature, since n it's declared
// as a class (static) property it is visible in every method of this class
int[] vetorA = generateVector();
// Make some other space
System.out.println();
// Show results
for (int i = 0; i < vetorA.length; i++){
System.out.println("Number "+ vetorA[i] +" has Position: "+ i);
}
}
// The method is intended for internal use
// So you can keep this private too.
private static int[] generateVector(){
int[] vetorA = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Insert a number into the vector: ");
vetorA[i] = dados.nextInt();
}
return vetorA;
}
}
import java.util.Scanner;
公共类ADIDE02{
//为属性添加了专用访问修饰符。
//这里没有必要,但作为一般规则,尽量不允许直接访问
//如果可能,请初始化属性。
//改用访问器方法,这是一个好习惯
专用静态扫描仪dados=新扫描仪(System.in);
私有静态int n=0;
//主要
公共静态void main(字符串参数[]){
//询问向量大小
System.out.print(“定义向量大小:”);
n=dados.nextInt();
//腾出一些空间
System.out.println();
//更改了方法签名,因为它已声明
//作为类(静态)属性,它在该类的每个方法中都是可见的
int[]vetorA=generativector();
//腾出一些空间
System.out.println();
//显示结果
for(int i=0;i
此外,当命名变量符合Java命名约定时,只有类以大写字母开头。尝试按如下方式修改类:
import java.util.Scanner;
public class Atividade02 {
// Added private access modifiers for properties.
// It's not necessary here, but as a general rule, try to not allow direct access to
// class properties when possible.
// Use accessor methods instead, it's a good habit
private static Scanner dados = new Scanner(System.in);
private static int n = 0;
// Main
public static void main(String args[]){
// Ask for vector size
System.out.print("Define vector size: ");
n = dados.nextInt();
// Make some space
System.out.println();
// Changed the method signature, since n it's declared
// as a class (static) property it is visible in every method of this class
int[] vetorA = generateVector();
// Make some other space
System.out.println();
// Show results
for (int i = 0; i < vetorA.length; i++){
System.out.println("Number "+ vetorA[i] +" has Position: "+ i);
}
}
// The method is intended for internal use
// So you can keep this private too.
private static int[] generateVector(){
int[] vetorA = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Insert a number into the vector: ");
vetorA[i] = dados.nextInt();
}
return vetorA;
}
}
import java.util.Scanner;
公共类ADIDE02{
//为属性添加了专用访问修饰符。
//这里没有必要,但作为一般规则,尽量不允许直接访问
//如果可能,请初始化属性。
//改用访问器方法,这是一个好习惯
专用静态扫描仪dados=新扫描仪(System.in);
私有静态int n=0;
//主要
公共静态void main(字符串参数[]){
//询问向量大小
System.out.print(“定义向量大小:”);
n=dados.nextInt();
//腾出一些空间
System.out.println();
//更改了方法签名,因为它已声明
//作为类(静态)属性,它在该类的每个方法中都是可见的
int[]vetorA=generativector();
//腾出一些空间
System.out.println();
//显示结果
for(int i=0;i