三道JAVA上机编程题,求大神帮忙,做了很久,没做出来
发布网友
发布时间:2022-04-21 15:38
我来回答
共6个回答
热心网友
时间:2023-07-01 15:21
你出错的语句是在 int number = input.nextInt()这一块,你只把你输入的第一个int赋给了number,后面的int想要继续赋给number得通过循环啊。main方法体修改如下:
public static void mian()
{
System.out.println("blabla");
Scanner input = new Scanner(System.in);
int number;
int count;
int count1 = 0;
int count2 = 0;
int total = 0;
while(input.hasNextInt())
{
number = input.nextInt();
if(number!=0)
{
//将你所给代码中的对应if体内代码照抄即可
//.......
}else{
//如果一旦碰到0就退出,即便0后面还有数字;若要求不是这样,注释掉break
break;
}
}
//将if体外的代码照抄至此
//……
}
热心网友
时间:2023-07-01 15:22
int fushu=0;
int zhengshu=0;
int sum=0;
ArrayList list = new ArrayList();//假设你把数据加到list里面
list.Add(1);
list.Add(2);
//计算正负个数
for (int i = 0; i < list.Count;i++ )
{
if (Convert.ToInt32(list[i]) < 0)
fushu++;
if (Convert.ToInt32(list[i]) > 0)
zhengshu++;
else break;//自己加提示停止运行
sum =sum+ Convert.ToInt32(list[i]);
}
Console.WriteLine("整数个数:" + zhengshu + "\r\n" + "负数个数:" + fushu + "\r\n" + "总和:" + sum + "\r\n");
C#写的 讲究一下吧
热心网友
时间:2023-07-01 15:22
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream.GetField;
import java.sql.Timestamp;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class MainTest {
public static void main(String[] args) throws NumberFormatException, IOException {
int n = -1;
int zheng = 0,fu =0;
int all = 0;
float avg =0.0f;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
List<Integer> numbers = new ArrayList<Integer>();
while(n != 0)
{
n = Integer.valueOf(in.readLine());
numbers.add(n);
}
for(int i=0;i<numbers.size();i++)
{
if(numbers.get(i)>0)
{
zheng++;
}
else if(numbers.get(i) <0)
{
fu++;
}
all += numbers.get(i);
}
avg = all/numbers.size();
System.out.println("正数"+zheng);
System.out.println("负数"+fu);
System.out.println("总和"+all);
System.out.println("平均值"+avg);
}
}
热心网友
时间:2023-07-01 15:23
int x=(int)System.in.read();
int sum=0,count=0,Positive=0,negative=0;
while(x!=0){
if(x>0){
Positive++;
}else{
negative++;
}
sum+=x;
x=(int)System.in.read();
}
System.out.println("sum="+sum+“;average=”+(sum/(Positive+negative)+";count="+(Positive+negative)));
热心网友
时间:2023-07-01 15:24
package ;
import java.util.Scanner;
/**
* @author YUGI KONAMI
*/
public class CalcNumbers
{
public static void main ( String[] args )
{
System.out.println ("输入一行数字:(0个或多个空格分开)");
Scanner s = new Scanner (System.in);
String line = null;
while (s.hasNextLine ())
{
line = s.nextLine ().trim ();
if ("0".equals (line))
{
s.close ();
break;
}
if (!line.matches ("^((\\+|\\-)?\\d+\\s*)+$"))
{
System.err.println ("输入格式不正确 ");
}
else
{
String reg3 = "\\s+";
String[] strs = line.split (reg3);
int sum = 0;
int z = 0, f = 0;
for ( String string : strs )
{
if (!"".equals (string))
{
if (string.startsWith ("+"))
{
z++;
string = string.substring (1);
}
else if (string.startsWith ("-"))
{
f++;
}
else
{
z++;
}
sum += Integer.parseInt (string);
}
}
System.out.println ("正数有 " + z + " 个");
System.out.println ("负数有 " + f + " 个");
System.out.println ("和是:" + sum + " ,avg是: " + sum / ( strs.length - 1 ));
}
}
}
}
热心网友
时间:2023-07-01 15:24
import java.util.Scanner;
public class Calc {
private Scanner scanner=new Scanner(System.in);
private int posNum=0;
private int negNum=0;
private int sum=0;
private int number=0;
private int num=0;
public Calc(){
while(true){
System.out.print("请输入数字:");
number=scanner.nextInt();
if(number==0){
break;
}else if(number<0){
negNum++;
}else {
posNum++;
}
num++;
sum+=number;
}
System.out.println("输入结束,结果如下:");
System.out.println("正数个数:"+posNum);
System.out.println("负数个数:"+negNum);
System.out.println("总和:"+sum);
System.out.println("平均数:"+(sum/num));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Calc();
}
}