当输入的标记为负数时,如何获取获取无效标记输出的代码?
class Main {
public static void main(String[] args) {
int i;
int[] a = new int[6];
Scanner s = new Scanner(System.in);
System.out.println("");
for (i = 0; i < 6; i++) {
a[i] = s.nextInt();
}
for (i = 0; i < 6; i++) {
if (a[i] < 0) {
System.out.println("Invalid mark");
} else {
if (a[i] > 0) {
int max = a[0];
for(i = 0; i < 6; i++) {
if (max < a[i]) {
max = a[i];
}
}
System.out.println("Highest mark is " + max);
}
}
}
}
}
答案 0 :(得分:-1)
有用于删除数组中元素的代码,请参见下面的静态方法:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
int i;
int a[]=new int[6];
Scanner s= new Scanner(System.in);
System.out.println("");
for( i=0;i<6;i++)
a[i]=s.nextInt();
int max=-1;
for(i=0;i<6;i++)
{
if(a[i]<0)
System.out.println("Invalid mark");
else
if (max<a[i])
max=a[i];
}
if(max>=0)
System.out.println("Highest mark is "+max);
else
System.out.println("No max as there are only invalid inputs!");
}
}
/** METHOD TO REMOVE AN INTEGER FROM AN ARRAY
*@ param elem , intArr
*/
public static void remove(int elem, int[] intArr)
{
for(int i = 0; i < intArr.length; i++){
if(intArr[i] == elem){
for(int j = i; j < intArr.length - 1; j++){
intArr[j] = intArr[j+1];
}
break;
}
}
}