一个JAVA编程题
- 提问者网友:太高姿态
- 2021-04-23 10:17
- 五星知识达人网友:怀裏藏嬌
- 2021-04-23 10:35
public class test {
public static void main( String[] args ) {
int[] a = {8,4,2,1,23,344,12};
int sum = 0;
for ( int i : a ) {
System.out.print( i + " " );
sum += i;
}
System.out.println( "\n所有数的和是: " + sum );
System.out.println( "请输入一个数: " );
Scanner s = new Scanner( System.in );
int n = s.nextInt();
boolean f = false;
for ( int i : a ) {
if ( n == i ) {
f = true;
break;
}
}
if ( f ) {
System.out.println( "包含" );
} else {
System.out.println( "不包含" );
}
}
}
- 1楼网友:未来江山和你
- 2021-04-23 12:36
import java.util.Scanner;
public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //扫描器,扫描从键盘输入的值 int[] numArr = {8,4,2,1,23,344,12}; int result = 0; //保存和 System.out.print("数列值为:"); for(int i=0;i<numArr.length;i++) { //循环输出数组值 System.out.print(numArr[i] + "\t"); result = result + numArr[i]; } System.out.println("\n"+"数列的和为:"+result); int num = sc.nextInt(); boolean tag = false; System.out.print("数字:"+num); for(int j=0;j<numArr.length;j++) { if(num == numArr[j]) { System.out.println("在数列中!"); break; }else { if(j == (numArr.length-1)) {//防止重复输出,再做一次判断 System.out.println("不在数列中!"); } } } } }
水平有限,代码难免不够完善。有什么问题可以联系我。
- 2楼网友:醉吻情书
- 2021-04-23 12:09
import java.io.IOException; import java.util.Scanner;
public class abc { public static void main(String[] args) { int a[]={8,4,2,1,23,344,12}; int sum=0; for(int i=0;i<a.length;i++) { System.out.println(a[i]); sum=sum+a[i]; } System.out.println("总和:"+sum); int x=0; System.out.print("请输入一个数字!"); Scanner sc=new Scanner(System.in); x=sc.nextInt(); int aa=1; for(int i=0;i<a.length;i++) { if(x==a[i]) { aa=2; } } if(aa==2) { System.out.println(x+"是这个数列的值"); }else{ System.out.println(x+"不是这个数列的值"); } }
}