java初学者求助!关于数组,字符串方法的题目
答案:4 悬赏:20 手机版
解决时间 2021-05-19 10:09
- 提问者网友:别再叽里呱啦
- 2021-05-19 02:30
编写程序,从键盘输入
10个整数存入数组,完成下列任务
1,打印输出该数组中最大值最小值及他们在数组中的位置
2,打印输出这10个数的和
3,打印所有大于30的数
4,求出这10个数的平均值并打印出小于该平均值的数
题目就是这样的,java快考试了,还是很不会。。。在书后习题上找到这么一个题目,觉得问题很范例,想把它当作例题看,但是没有答案。希望可以有人帮助一下!感激不尽!(另外,从键盘输入我们只学过开头加
import java.io.*;
然后用 BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); 的这句)
最佳答案
- 五星知识达人网友:平生事
- 2021-05-19 03:33
就一个键盘接受 为什么要用IO呢?
System.in
然后使用它scanner这个类就可以完成控制台接受数据了
输出最大最小对数组进行一个冒泡排序记录一下索引然后输出第一个和最后一个就可以实现了
剩下的问题 全部都是遍历数组然后输出的方法
第四个问题 可以将第二个问题的解决办法进行封装
然后调用除10就可以了
在比较输出
好好学JAVA吧
光写出代码给你
你看过也不见得会
要学的是思路
呵呵
全部回答
- 1楼网友:纵马山川剑自提
- 2021-05-19 05:06
import java.io.*;
public class A {
public static void main( String[] args ) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
int[] a = new int[10];
int mins = 0, maxs = 0;
int sum = 0;
double avg;
System.out.println( "请输入10个数,每行一个:" );
for ( int i = 0; i < a.length; ++i ) {
sum += a[i] = Integer.parseInt( in.readLine() );
if ( a[min] > a[i] )
mins = i;
else if ( a[maxs] < a[i] )
maxs = i;
}
System.out.println( "最大数:" + a[maxs] + " 位置:" + maxs );
System.out.println( "最小数:" + a[mins] + " 位置:" + mins );
System.out.println( "和:" + sum );
for ( int i = 0; i < 10; ++i ) {
if ( a[i] > 30 )
System.out.print( a[i] + " " );
}
System.out.println();
avg = sum / 10;
for ( int i = 0; i < 10; ++i ) {
if ( a[i] < avg )
System.out.print( a[i] + " " );
}
}
}
- 2楼网友:刀戟声无边
- 2021-05-19 04:37
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class pailieshuzu {
public static void h1(String str)//1,打印输出该数组中最大值最小值及他们在数组中的位置
{
String[] st = str.split(" ");
int b=Integer.parseInt(st[0]);
int b1=0;
int s=Integer.parseInt(st[0]);
int s1=0;
for(int i=0;i<st.length;i++)
{
if(b<Integer.parseInt(st[i]))
{
b=Integer.parseInt(st[i]);
b1=i;
}
if(s>Integer.parseInt(st[i]))
{
s=Integer.parseInt(st[i]);
s1=i;
}
}
System.out.println("最大的数字是:"+b+" 在数组中的位置是str["+b1+"] 最小的数字是:"+s+" 在数组中的位置是:str["+s1+"]");
}
public static int h2(String str)//2,打印输出这10个数的和
{
String[] st = str.split(" ");
int sum=0;
for(int i=0;i<st.length;i++)
{
sum = sum+Integer.parseInt(st[i]);
}
System.out.println("数组当中的数字之和等于:"+sum);
return sum;
}
public static void h3(String str)//打印所有大于30的数
{
String[] st = str.split(" ");
System.out.print(" 数组当中大于30的数:");
for(int i=0;i<st.length;i++)
{
if(Integer.parseInt(st[i])>30)
{
System.out.print(st[i]);
}
}
System.out.println("");
}
public static void h4(String str)//4,求出这10个数的平均值并打印出小于该平均值的数
{
String[] st = str.split(" ");
int sum=h2(str)/st.length;
System.out.print(" 小于数组平均数["+sum+"]的有:");
for(int i=0;i<st.length;i++)
{
if(Integer.parseInt(st[i])<sum)
{
System.out.print(" "+st[i]);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("请输入数字并且用空格分开~");
String str = in.readLine();
//1 2 3 4 50 6 7 8 9
if(str!=null&&str!="")
{
h1(str);//1,打印输出该数组中最大值最小值及他们在数组中的位置
h2(str);//2,打印输出这10个数的和
h3(str);//打印所有大于30的数
h4(str);//4,求出这10个数的平均值并打印出小于该平均值的数
}
} catch (IOException e) {
e.printStackTrace();
}
}
}测试结果
import java.io.*;
public class test {
public static void main( String[] args ) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
int[] a = new int[10];
int min = 0, max = 0;
int sum = 0;
double avg;
System.out.println( "请输入10个数,每行一个:" );
for ( int i = 0; i < 10; ++i ) {
sum += a[i] = Integer.parseInt( in.readLine() );
if ( a[min] > a[i] )
min = i;
else if ( a[max] < a[i] )
max = i;
}
System.out.println( "最大数:" + a[max] + " 位置:" + max );
System.out.println( "最小数:" + a[min] + " 位置:" + min );
System.out.println( "和:" + sum );
for ( int i = 0; i < 10; ++i ) {
if ( a[i] > 30 )
System.out.print( a[i] + " " );
}
System.out.println();
avg = sum / 10;
for ( int i = 0; i < 10; ++i ) {
if ( a[i] < avg )
System.out.print( a[i] + " " );
}
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯