You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:为判断画作横竖版的控制台程序添加循环重启功能

Fixing Your Painting Orientation Program

Hey there! Let's sort out your code to handle invalid inputs correctly and fix the orientation logic at the same time.

First, a quick note: your current orientation check is reversed! When a painting's height is greater than its width, that's Portrait mode (think a tall vertical painting), not Landscape. We'll correct that first.

This approach uses a reusable helper method to prompt for valid integers, so users only re-enter the specific invalid value instead of starting over entirely. We also add an optional loop to let users check multiple paintings:

class Program {
    static void Main(string[] args) {
        bool keepChecking = true;
        
        // Outer loop to check multiple paintings
        while (keepChecking) {
            // Get valid height and width using our helper method
            int height = GetValidDimension("Height");
            int width = GetValidDimension("Width");
            
            // Correct orientation logic
            var orientation = height > width ? Orientation.Portrait : Orientation.Landscape;
            Console.WriteLine($"Your Painting is in: {orientation} orientation");
            
            // Ask if user wants to check another painting
            Console.WriteLine("\nCheck another painting? (Y/N)");
            string response = Console.ReadLine().Trim().ToUpper();
            keepChecking = response == "Y";
        }
    }
    
    // Helper method to get a valid positive integer dimension
    static int GetValidDimension(string dimensionName) {
        int value;
        do {
            Console.WriteLine($"Enter the {dimensionName} of the Painting:");
            string input = Console.ReadLine();
            
            // Validate input is a positive integer
            if (int.TryParse(input, out value) && value > 0) {
                break; // Exit loop if valid
            }
            
            Console.WriteLine($"Invalid {dimensionName}! Please enter a positive integer (e.g., 1-9).");
        } while (true); // Loop until valid input is received
        
        return value;
    }
    
    public enum Orientation { Landscape, Portrait }
}

Key Improvements:

  • Reusable Helper Method: GetValidDimension handles all input validation for height/width, keeping your code DRY (Don't Repeat Yourself).
  • Targeted Re-Prompting: Users only re-enter the invalid dimension instead of starting the whole process over.
  • Positive Integer Check: Ensures users can't enter zero or negative numbers (which don't make sense for a painting's size).
  • Multiple Painting Support: The outer loop lets users check as many paintings as they want without restarting the program.
  • Corrected Orientation Logic: Now correctly identifies Portrait when height > width.

Option 2: Full Restart on Invalid Input

If you specifically want the program to restart entirely whenever any input is invalid (go back to asking for height), you can use an outer loop with continue to reset the process:

class Program {
    static void Main(string[] args) {
        while (true) {
            Console.WriteLine("What is the Height of the Painting?");
            string input = Console.ReadLine();
            int height;
            
            // Check if height is valid
            if (!int.TryParse(input, out height) || height <= 0) {
                Console.WriteLine("Invalid number. Please enter a positive integer... e.g 1-9");
                continue; // Restart the loop
            }
            
            Console.WriteLine("What is the Width of the Painting?");
            string input2 = Console.ReadLine();
            int width;
            
            // Check if width is valid
            if (!int.TryParse(input2, out width) || width <= 0) {
                Console.WriteLine("Invalid number. Please enter a positive integer... e.g 1-9");
                continue; // Restart the loop
            }
            
            // Correct orientation logic
            var orientation = height > width ? Orientation.Portrait : Orientation.Landscape;
            Console.WriteLine("Your Painting is currently in the: " + orientation + " orientation");
            
            // Exit after one successful check (remove this line to keep looping indefinitely)
            break;
        }
    }
    
    public enum Orientation { Landscape, Portrait }
}

This approach is simpler but less user-friendly because a mistake in entering width forces the user to re-enter height again.

内容的提问来源于stack exchange,提问作者AntiMeta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:54:20