You and your date are trying to get a table at a restaurant. The parameter "you" is the stylishness of your clothes, in the range 0..10, and "date" is the stylishness of your date's clothes. The result getting the table is encoded as an int value with 0=no, 1=maybe, 2=yes. If either of you is very stylish, 8 or more, then the result is 2 (yes). With the exception that if either of you has style of 2 or less, then the result is 0 (no). Otherwise the result is 1 (maybe).
时间: 2023-12-10 08:03:25 浏览: 131
Here's a possible implementation of the logic you described in Python:
```
def get_table(you, date):
if you <= 2 or date <= 2:
return 0
elif you >= 8 or date >= 8:
return 2
else:
return 1
```
This function takes two arguments, `you` and `date`, which are assumed to be integers in the range 0..10 representing the stylishness of your clothes and your date's clothes, respectively.
The function first checks if either of you has a style of 2 or less, in which case it immediately returns 0 (no table). Then it checks if either of you has a style of 8 or more, in which case it immediately returns 2 (yes table). Otherwise, it returns 1 (maybe table).
相关问题
If the user types something which is not an integer, then the nextInt method of the scanner will throw an InputMismatchException. In that case the code of your readPosInt method must catch the exception, use System.out.println to print the error message "You must type an integer!" to the user (use System.out.println for this, not System.err.println, otherwise you might hit a bug in Eclipse...), use the scanner’s nextLine method to read (and ignore) the wrong input typed by the user of the program (if you do not do this, the wrong input typed by the user will remain in the input stream, and the next time you call the nextInt method again, you will get an InputMismatchException again!), and then do the whole thing again (including printing again the string argument of the readPosInt method) to try to read an integer again (hint: put the whole code of the method inside a while loop).java代码
Sure, I understand your question. Here's a possible implementation of the `readPosInt` method in Java that handles the scenario you described:
```java
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n = readPosInt("Please enter a positive integer: ");
System.out.println("You entered: " + n);
}
public static int readPosInt(String prompt) {
Scanner scanner = new Scanner(System.in);
int n = 0;
boolean success = false;
while (!success) {
System.out.print(prompt);
try {
n = scanner.nextInt();
if (n > 0) {
success = true;
} else {
System.out.println("You must type a positive integer!");
}
} catch (InputMismatchException e) {
System.out.println("You must type an integer!");
scanner.nextLine(); // consume wrong input
}
}
return n;
}
}
```
In this implementation, we use a `while` loop to keep trying to read an integer until we succeed. Inside the loop, we print the prompt and attempt to read an integer using `scanner.nextInt()`. If this call throws an `InputMismatchException`, we catch it and print the error message "You must type an integer!" to the user using `System.out.println()`. We also use `scanner.nextLine()` to consume the wrong input typed by the user, so that the input stream is properly cleared and the next call to `scanner.nextInt()` doesn't fail again.
If the input is an integer, we check if it's positive and set the `success` flag to `true` if it is. If the input is not positive, we print the error message "You must type a positive integer!" to the user using `System.out.println()`.
Finally, if we succeed in reading a positive integer, we return it from the method.
InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings. UnityStandardAssets.CrossPlatformInput.PlatformSpecific.StandaloneInput.GetA
pKey is causing the InvalidOperationException because you are trying to read input using the old UnityEngine.Input class, but you have switched to the new Input System package in Player Settings. To fix this issue, you need to update your code to use the new Input System API for reading input.
Here's an example of how you can read input using the new Input System:
```csharp
using UnityEngine;
using UnityEngine.InputSystem;
public class YourScript : MonoBehaviour
{
private PlayerControls controls;
private void Awake()
{
controls = new PlayerControls();
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
private void Update()
{
float inputValue = controls.YourActionMap.YourInputControl.ReadValue<float>();
// Do something with the input value
}
}
```
In this example, `YourActionMap` refers to the action map you have defined in the Input System settings, and `YourInputControl` refers to the specific input control you want to read.
Make sure you have properly set up your Input System settings in the Unity Editor and defined the necessary action maps and input controls.
阅读全文