如何仅计算数组中的整数类型?

huangapple go评论67阅读模式
英文:

How to calculate only integer types in array?

问题

以下是您的代码的翻译部分:

import java.util.*;
class Distance {
	private String name;
	private int dist;
	public Distance(String name, int dist) {
		this.name = name;
		this.dist = dist;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getDist() {
		return dist;
	}
	public void setDist(int dist) {
		this.dist = dist;
	}
	public String toString() {
		return "Distance [name=" + name + ", school street=" + dist + "]";
	}
}
class DistanceComp {
	public static Distance longdistance(Distance[] dim) {
		Distance max = dim[0];
		for (int i = 1; i < dim.length; i++) {
			if (max.getDist() < dim[i].getDist())
				max = dim[i];
		}
		return max;
	}
	public static Distance shortdistance(Distance[] dim) {
		Distance min = dim[0];
		for (int i = 1; i < dim.length; i++) {
			if (min.getDist() > dim[i].getDist())
				min = dim[i];
		}
		return min;
	}
}
public class week03_01 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		Distance[] dist = new Distance[3];
		System.out.print(">> how many students? : ");
		int num = in.nextInt();
		for (int i = 0; i < num; i++) {
			System.out.print(">> name and distance : ");
			dist[i] = new Distance(in.next(), in.nextInt());
		}
		System.out.println("\na student with the longest commute to school : " + DistanceComp.longdistance(dist));
		System.out.println("a student with the shortest commute to school  : " + DistanceComp.shortdistance(dist));
		System.out.println("school distance difference is "); // 这里有一个未完成的输出语句
	}
}

关于计算学校距离之间的差异,您需要编写代码来计算这个差异。您可以在循环中找到最大和最小的距离,然后计算它们之间的差异。这是一个示例代码片段,用于计算差异:

int maxDistance = DistanceComp.longdistance(dist).getDist();
int minDistance = DistanceComp.shortdistance(dist).getDist();
int distanceDifference = maxDistance - minDistance;

System.out.println("school distance difference is " + distanceDifference);

这个代码段找到了最长和最短的距离,然后计算它们之间的差异,并将结果打印出来。

英文:
import java.util.*;
class Distance {
private String name;
private int dist;
public Distance(String name, int dist) {
this.name = name;
this.dist = dist;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDist() {
return dist;
}
public void setDist(int dist) {
this.dist = dist;
}
public String toString() {
return &quot;Distance [name=&quot; + name + &quot;, school street=&quot; + dist + &quot;]&quot;;
}
}
class DistanceComp {
public static Distance longdistance(Distance[] dim) {
Distance max = dim[0];
for (int i = 1; i &lt; dim.length; i++) {
if (max.getDist() &lt; dim[i].getDist())
max = dim[i];
}
return max;
}
public static Distance shortdistance(Distance[] dim) {
Distance min = dim[0];
for (int i = 1; i &lt; dim.length; i++) {
if (min.getDist() &gt; dim[i].getDist())
min = dim[i];
}
return min;
}
}
public class week03_01 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Distance[] dist = new Distance[3];
System.out.print(&quot;&gt;&gt; how many students? : &quot;);
int num = in.nextInt();
for (int i = 0; i &lt; num; i++) {
System.out.print(&quot;&gt;&gt; name and distance : &quot;);
dist[i] = new Distance(in.next(), in.nextInt());
}
System.out.println(&quot;\na student with the longest commute to school : &quot; + DistanceComp.longdistance(dist));
System.out.println(&quot;a student with the shortest commute to school  : &quot; + DistanceComp.shortdistance(dist));
System.out.println(&quot;school distance difference is &quot; + );
}
}

I also want to print the "shool distance differecnce".
but it doesn't calculate. i think it has String types.

I think calculate only integer types in an array, but i don't know the code.

Or is s there any other way? Ask for advice.

答案1

得分: 2

在你的 DistanceComp 类中,创建一个方法来减去两个距离,类似于你为 longdistanceshortdistance 所做的:

public static int subtractDistance(Distance dist1, Distance dist2) {
    int difference = Math.abs(dist1.getDist() - dist2.getDist());
    return difference;
}

然后,在你的 System.out 中使用它:

System.out.println("school distance difference is " + DistanceComp.subtractDistance(DistanceComp.longdistance(dist),  DistanceComp.shortdistance(dist)));

一些附注供参考:

  1. 你的代码目前只适用于3个学生。
  2. 不要使用过长的变量名,将它们赋值给一个命名较短的变量。这有助于提高代码的可读性。
英文:

In your DistanceComp class, create a method to subtract two Distances similar like you did for longdistance and shortdistance:

    public static int subtractDistance(Distance dist1, Distance dist2) {
int difference = Math.abs(dist1.getDist() - dist2.getDist());
return difference;
}

Then, use that in your System.out:

System.out.println(&quot;school distance difference is &quot; + DistanceComp.subtractDistance(DistanceComp.longdistance(dist),  DistanceComp.shortdistance(dist)));

Some notes fyi:

  1. Your code currently only works with 3 students.
  2. Instead of using long names, assign them to a shorter-named variable. This helps with code readability.

huangapple
  • 本文由 发表于 2020年9月20日 10:23:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63974983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定