【发布时间】:2015-01-24 07:42:44
【问题描述】:
我正在完成一个本周到期的项目。它是一个按升序获取数组的交集和差的程序。就方法而言,一切都在工作,但我试图花哨并在用户响应之间添加一个换行符,但我一定在某个地方删除了一些东西并且无法找到我的日志到那个部分。它假设只是重复 do while 循环中的内容,但是当我第二次再去时,它看起来像是在乘以我应该放入 int 的数字集。我知道这一定是一个简单的逻辑错误,但此时我的脑袋已经炸了。
这是我目前得到的:
import java.util.Scanner;
public class settest {
public static Scanner kbd = new Scanner(System.in);
/*
* This method will retrieve "size" number of unique
* integers from keyboard and placed into an array set.
*/
public static void getData(int [] set, int size){
boolean isUnique=false;
int input; int count=0;
while(count < size){
input = kbd.nextInt();
for(int i=0; i < size; i++)
{
if (input == set[i])
{
isUnique = true;
}
}
if (!isUnique)
{
set[count] = input;
count++;
}
isUnique = false;
}
}
/*
* This method will calculate the intersection of two sets of arrays.
* It needs to receive two sets of arrays, their size, and a holding
* array as parameters from the main program.
* When finished the method will return the numbers that are in common
* in both sets (resultSize)
*/
public static int intersection(int [] setA, int sizeA , int [] setB,
int sizeB, int [] resultSet){
int resultSize=0;
for(int x=0; x<sizeA; x++)
{
for(int y=0; y<sizeB; y++)
{
if(setA[x]==setB[y])
{
resultSet[resultSize]=setA[x];
resultSize++;
}
}
}
return resultSize;
}
/*
* Calculates the difference (A-B) of the two array sets.
* It will received the set [], size of correct array and in
* ascending order as parameters.
* Returns the correct array size for the received resultSet[]
*/
public static int difference(int [] setA, int sizeA , int [] setB,
int sizeB, int [] resultSet){
int resultSize=0; boolean sameVal=false;
for(int x=0; x<sizeA; x++ ){
for(int y=0; y<sizeB; y++){
if(setA[x]==setB[y]){
sameVal=true;
}
}
if(!sameVal){
resultSet[resultSize]=setA[x];
resultSize++;
}
sameVal=false;
}
return resultSize;
}
/*
* Method will sort the numbers in set[] array into ascending order.
*/
public static void sort(int [] set, int size){
for(int x=0; x< size-1; x++)
{
for(int y=x+1; y<size; y++)
{
if(set[x] > set[y])
{
int temp = set[x];
set[x] = set[y];
set[y] = temp;
}
}
}
}
public static final int MAXSIZE = 20;
public static void main(String[] args) {
int [] setA = new int[MAXSIZE];
int [] setB = new int[MAXSIZE];
int [] resultSet = new int[MAXSIZE];
int sizeA=0, sizeB=0;int resultSize=0;
String repeat;
do{
System.out.println("ENTER 2 SETS OF NUMBERS.\n");
System.out.println("HOW MANY NUMBERS IN YOUR FIRST SET?");
sizeA=kbd.nextInt();
for(int i=0; i<sizeA; i++){
if (sizeA>MAXSIZE){
System.out.println("\nERROR: SET SIZE LIMIT EXCEEDED."
+ "ENTER A DIFFERENT AMOUNT. \n");
sizeA = kbd.nextInt();
}
}
System.out.println("\nENTER THE FIRST SET OF NUMBERS");
getData(setA, sizeA);
sort(setA,sizeA);
System.out.println("\nHOW MANY NUMBERS IN YOUR SECOND SET?");
sizeB=kbd.nextInt();
for(int i=0; i<sizeB; i++){
if (sizeB>MAXSIZE){
System.out.println("\nERROR: SET SIZE LIMIT EXCEEDED. \n"
+ "ENTER A DIFFERENT AMOUNT.\n ");
sizeB = kbd.nextInt();
}
}
System.out.println("\nENTER THE SECOND SET OF NUMBERS");
getData(setB, sizeB);
sort(setB, sizeB);
System.out.println("\nINTERSECTION:");
resultSize=intersection(setA,sizeA,setB,sizeB, resultSet);
for(int i=0; i<resultSize; i++){
System.out.print(resultSet[i] + " ");
}
System.out.println("\n\nDIFFERENCE OF A-B:");
resultSize=difference(setA,sizeA,setB,sizeB, resultSet);
for(int i=0; i<resultSize; i++){
System.out.print(resultSet[i]+" ");
}
System.out.println("\n\nDIFFERENCE OF B-A:");
resultSize=difference(setB,sizeB,setA,sizeA, resultSet);
for(int i=0; i<resultSize; i++){
System.out.print(resultSet[i]+" ");
}
System.out.println("\n\nDO YOU WANT TO CONTINUE?");
repeat= kbd.next().toLowerCase();
} while(repeat.equals("y"));
}
}
任何帮助将不胜感激。
ENTER 2 SETS OF NUMBERS.
您的第一组有多少个数字? 3
输入第一组数字 4 3 2
第二组有多少个数字? 2
输入第二组数字 5 6
交叉路口:
A-B 的区别: 2 3 4
B-A 的区别: 5 6
您要继续吗? 是 输入 2 组数字。
您的第一组有多少个数字? 1
输入第一组数字 6
第二组有多少个数字? 6
输入第二组数字 3 5 4 1 0 9 7
交叉路口:
A-B 的区别: 6
B-A 的区别: 1 3 4 5 7 9
您要继续吗? 8 再见
在循环中的第二次循环中,用户数量会以某种方式递增到广告 1 或一对
【问题讨论】:
-
如果它是 netbeans,你使用了哪个 ide,你可以恢复你的代码
-
请看
http://stackoverflow.com/a/12579781/1262764 -
它不工作。我没有这些选项。
-
请为您的应用发布示例输入和所需的输出,以便对其进行调试。