![]() ![]() # If the list is empty, it will print the below statement. Write a program that prints the smallest and largest positive double value. Using Math.random() double random Math.random() 49 + 1 or int random (int )(Math.random() 50 + 1) This will give you value from 1 to 50 in case of int or 1.0 (inclusive) to 50.0 (exclusive) in case of double. # If the list is not empty, it will print the max and min numbers. Print the reciprocal as a hexadecimal floating-point number. ![]() # added an if statement to check if the list is empty. # the user's input will be appended in num_list. The initial value of the maximum should be Integer. Declare two int s, one to hold the maximum value, the other the minimum value. UserNumber = input("Please enter a number. Instructions for finding the maximum and minimum of a 2D int array using Arrays.sort (): Declare a 2D int array to sort called data. # create an empty list where the user input will be stored. With a try/except and put out an appropriate message and ignore the number.Įnter 7, 2, bob, 10, and 4 and match the output below.""" If the user enters anything other than a valid number catch it Print out the largest and smallest of the numbers. Then, you ask if num is a string with the value 'done' in order to break the loopĬheck out my answer: """5.2 Write a program that repeatedly prompts a user for integer numbers You need to enter here only if the command "int(num)" throws an error. n + 1 Another solution is using Math. Remember that casting a double value to integer (int) will throw away any values after the decimal point. The code below will create a random integer from 0 to 9. int n rand.nextInt (50) // Add 1 to the result to get a number from the required range // (i.e., 1 - 50). Activity: 2.9.1 ActiveCode (random1) You can use Math.random and a cast to integer to return a random integer between some starting and ending value. Largest = int(num) if largest = None or int(largest) int(num) else smallest 870 The first solution is to use the class: import Random rand new Random () // Obtain a number between 0 - 49. `Otherwhise` the first time you would compare an integer with a nonetype. In order to avoid the type error of the comparison between integer and NoneType you must 'cast' the integer type in the if/else statement. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Once 'done' is entered, print out the largest and smallest of the numbers. Random rand new Random() // nextInt as provided by Random is exclusive of the top value so you need to add 1 int randomNum rand.nextInt((max - min) + 1) + min See the relevant JavaDoc. ![]() Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |