首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我怎样才能使挑选升序和降序的问题成为错误的证明?

我怎样才能使挑选升序和降序的问题成为错误的证明?
EN

Stack Overflow用户
提问于 2019-03-21 03:19:51
回答 2查看 34关注 0票数 1

当我在第23行的if语句中放入一个不在其中的值时,我的代码将停止工作。我想知道如何使代码的if部分不出错。我知道这个修复将涉及一个for循环,但是我不知道从哪里开始。我让if循环告诉代码,如果order等于1,则以升序(expensesAscending)进行搜索;如果order等于2,则以降序(expensedescending)进行搜索。

代码语言:javascript
复制
package project;

import java.io.*;
import java.util.*;
import java.util.Scanner;

public class project {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); 
        int Size;//size of the array
        int order; // ascending or descending order
        int value;//this is for the value that you are looking for
        System.out.println("Put in the amount of expenses you have");
        Size = sc.nextInt();//User input for the amount of expenses
        System.out.println("put in all your expenses");
        int userInput[] = new int[Size];// what the users expenses are
        for (int i = 0; i < userInput.length; i++)//This loop is for if the i value is smaller than user input then put in more values to complete the array
            userInput[i] = sc.nextInt();
        System.out.println("do you want it ascending or descending order. If you want it in ascending press 1 or if you want descending press 2");
        order = sc.nextInt();// select if they wanted it in ascending or descending order
        System.out.print("expenses not sorted : ");
        printExpenses(userInput);//this is the method that prints out all the expenses
        if (order == 1) {
            expensesAscending(userInput);// If order is equal to one then sort in ascending else if it is equal to 2 then order it descending
        } else if (order == 2) {
            expensedescending(userInput);

        }
        System.out.println("what value are you looking for");
        value = sc.nextInt();
        if (order == 1) {int ans = binarySearchAscending(userInput, 0, Size-1, value);//use the binary search ascending method
        if(ans == -1)
            System.out.println("value not found");
          else
            System.out.println("your expense is found at "  + ans + " and the value of the array is " + userInput[ans]);
    }else if (order==2) {int ans = binarySearchDescending(userInput, 0, Size-1, value);//use the binary search descending method
    if(ans == -1)
        System.out.println("value not found");
      else
        System.out.println("your expense is found at " + ans + "and the value of the array is " + userInput[ans]);

    }}


    public static void printExpenses(int[] arr) {
        // this is were it is printed
        for (int i = 0; i < arr.length; i++) {//loops when i = to 0 and i is less than the length of the array then you should add one to the i value so that it could print out the entire array
            System.out.println(arr[i] + "$");
        }
    }

    public static void expensedescending(int arr[]) {
        // This is were the selection sort starts
        int N = arr.length;
        for (int i = 0; i < N; i++) {
            int small = arr[i];
            int pos = i;
            for (int j = i + 1; j < N; j++) {
                if (arr[j] > small) {
                    small = arr[j];
                    pos = j;
                }
            }
            int temp = arr[pos];
            arr[pos] = arr[i];
            arr[i] = temp;
            System.out.println(": ");
            // Printing array after pass
            printExpenses(arr);
        }
    }

    public static void expensesAscending(int arr[]) {
        //insertion sort
        int N = arr.length;
        for (int i = 1; i < N; i++) {
            int j = i - 1;
            int temp = arr[i];
            while (j >= 0 && temp < arr[j]) {
                arr[j + 1] = arr[j];
                j--;
                ;
            }
            arr[j + 1] = temp;
            System.out.println(": ");
            // Printing array after pass
            printExpenses(arr);
        }
    }
    static int binarySearchAscending(int[] array, int left, int right, int key) {
        if (left > right) {
          return -1;
        }

        int mid = (left + right) / 2;

        if (array[mid] == key) {
          return mid;
        }

        if (array[mid] > key) {
          return binarySearchAscending(array, left, mid - 1, key);
        }

        return binarySearchAscending(array, mid + 1, right, key);
      }


    static int binarySearchDescending(int[] array, int left, int right, int key) {
        if (left > right) {
          return -1;
        }

        int mid = (left + right) / 2;

        if (array[mid] == key) {
          return mid;
        }

        if (array[mid] > key) {
          return binarySearchDescending(array, mid + 1, right, key);
        }

        return binarySearchDescending(array, left, mid - 1, key);
      }
}
EN

回答 2

Stack Overflow用户

发布于 2019-03-21 03:51:04

如果执行sc.nextInt()并提供一个非整数(在本例中,表示某个字符),则会得到InputMismatchException

为了进行此错误证明,您必须使用try-catch块捕获异常。

您可以对sc.nextInt()部件执行类似的操作。

代码语言:javascript
复制
try {
 order = sc.nextInt();
 //other lines
} catch (InputMismatchException e) {
 // do required processing here
}

现在,这处理了非整数。为了检查数字是否只有1和2,您可以使用while/do-while循环。

代码语言:javascript
复制
do {
try {
order = sc.nextInt();
//other lines
} catch (InputMismatchException e) {
// do required processing here
}
} while (1 != order && 2 != order);
票数 1
EN

Stack Overflow用户

发布于 2019-03-21 03:34:38

把它放在do-while中:

代码语言:javascript
复制
do {
    System.out.println("do you want it ascending or descending order. If you want it in ascending press 1 or if you want descending press 2");
    order = sc.nextInt();
} while (order != 1 && order != 2);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55268655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档